2012-08-31 1 views
5

크기가 조정되지 않는 대화 상자 을 만들려고합니다. 레이블이있는입니다. 이 레이블에는 많은 텍스트가 있으므로 대화 상자를 무분별하게 넓히지 않고 포장하려면 으로 지정하십시오..GTK 대화 상자에서 라벨 래핑

GTK가 이런 일이 일어나기까지 무엇이 필요한지 알 수 없습니다. 대화 상자에서 최대 너비를 설정하는 방법을 찾을 수조차 없으며 훌륭합니다. 여기

가 무슨 뜻인지의 실행 예입니다 : 채우기를 사용하여 Gtk.Table 내부 Gtk.Label 퍼팅 (True로 자동 줄 바꿈을 설정 외에)

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

from gi.repository import Gtk 

class DialogExample(Gtk.Dialog): 

    def __init__(self, parent): 
     Gtk.Dialog.__init__(self, "My Dialog", parent, 0, 
      (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, 
      Gtk.STOCK_OK, Gtk.ResponseType.OK)) 

     self.set_default_size(150, 100) 
     self.set_resizable(False) 

     label = Gtk.Label("This is a dialog to display additional information, with a bunch of text in it just to make sure it will wrap enough for demonstration purposes") 
     label.set_line_wrap(True) 

     box = self.get_content_area() 
     box.add(label) 
     self.show_all() 

class DialogWindow(Gtk.Window): 

    def __init__(self): 
     Gtk.Window.__init__(self, title="Dialog Example") 

     self.set_default_size(250, 200) 


     button = Gtk.Button("Open dialog") 
     button.connect("clicked", self.on_button_clicked) 

     self.add(button) 

    def on_button_clicked(self, widget): 
     dialog = DialogExample(self) 
     response = dialog.run() 

     if response == Gtk.ResponseType.OK: 
      print "The OK button was clicked" 
     elif response == Gtk.ResponseType.CANCEL: 
      print "The Cancel button was clicked" 

     dialog.destroy() 

win = DialogWindow() 
win.connect("delete-event", Gtk.main_quit) 
win.show_all() 
Gtk.main() 

답변

5

내가 이것을 해결 및 플래그를 축소하고 레이블의 고정 폭을 설정합니다. 이런 식으로 뭔가 :

label = Gtk.Label("This is a dialog to display additional information, with a bunch of text in it just to make sure it will wrap enough for demonstration purposes") 
label.set_line_wrap(True) 
label.set_size_request(250, -1) # 250 or whatever width you want. -1 to keep height automatic 

table = Gtk.Table(1, 1, False) 
table.attach(label, 0, 1, 0, 1, Gtk.AttachOptions.SHRINK | Gtk.AttachOptions.FILL) 

사용할 수있는 2.6 이후 트릭

+0

나는 문제가 무엇인지 깨달았다 조금을 파고. 창/대화 상자를 만들 때 라벨이 부모 크기 참조를 갖지 않을 때 처음에는 표시되지 않았기 때문에 Gtk은 레이블에 가능한 한 많은 공간을 할당 한 다음 상위 너비를 설정하여 거대한 창문. 이 동작을 피하려면 부모의 기본 너비를 설정하고 쇼를 수행하십시오. 이렇게하면 Gtk에서 부모 기하학을 계산하고 레이블에 부모 크기의 실제 참조가 있습니다. 나는 그것을 이렇게했다. 그리고 지금 모든 것이 매력처럼 작동하고있다. – satanas

+0

안녕하세요 @ satanas - "부모님이 선호하는 너비를 설정하고 공연을하는 방법"에 대해 조금 더 설명해 주실 수 있습니까? 나는 똑같은 문제가있다. Gtk.Table 옵션은 v3.4부터 사용되지 않으므로 비표시되지 않은 솔루션을 찾고 있습니다. TIA – fossfreedom

관련 문제