def test2(): top = tix.Tk() # ScrolledText小部件是标准Text小部件的扩展,它可以显示图片以及格式化的文本。tix版本的ScrolledText小部件 # 自动添加了滚动条,使用标准工具包来实现这一功能会产生很大的工作量。在使用方法上,它与其他Tkinter小部件非常相似 # 注意,虽然所有文本都显示在Tix的scrolledText小部件中,但是实际上使用了底层的标准Tkinter Text小部件来处理文本 st = tix.ScrolledText(top, width=300, height=100) st.pack(side="left") # 可以手动在文本框中输入或以编程的方式插入文本 # 使用了subwidget()方法来获得底层文本小部件的一个引用,然后使用它的insert()方法插入文本。 t = st.subwidget("text") t.insert("0.0", "Some inserted text") t.insert("end", "\n more inserted text") # 也可以在文本小部件中选中文本区域 # tag_configure()方法新建了一个tag,也就是文本的标签。标签被称为newfont并使用加粗的16号Roman字体。 # 在Tkinter中,这是标准的三字体说明符格式(因此Tix和ttk也支持) t.tag_configure("newfont", font=("Roman", 16, "bold")) # 注意:Tkinter中的文本索引使用包含格式化成浮点数的字符串。但实际上,它包含了用点分隔的行和列坐标。行开始于1,但列开始于0. # 所以1.0是第一行的第一个字符。可以使用字符串end或预定义的常量tkinter.END来标识行的结束,或者所有文本的结束。 s = t.get("1.0", "1.end") t.delete("1.0", "1.end") t.insert("1.0", s, "newfont") top.mainloop()
def makelist(self): stext = tix.ScrolledText(self.t, width=800, height=1000) stext.subwidget('text').insert(tix.INSERT, '\n'.join(rcc.creature['Raws'])) stext.grid(row=0, column=0) self.bb = tix.ButtonBox(self.t, orientation=tix.VERTICAL) self.bb.add('close', text='Close', command=self.close) self.bb.grid(row=0, column=1)
def main(): top = tix.Tk() nb = tix.NoteBook(top, width=300, height=200) nb.pack(expand=True, fill="both") nb.add("page1", label="text") f1 = tix.Frame(nb.subwidget("page1")) st = tix.ScrolledText(f1) st.subwidget("text").insert("1.0", "Here is where the text goes...") st.pack(expand=True) f1.pack() nb.add("page2", label="Message Boxes") f2 = tix.Frame(nb.subwidget("page2")) # 通过联合使用expand,fill和anchor,在窗口大小改变时,可以精确地控制小部件的行为 tix.Button( f2, text="error", bg="lightblue", command=lambda t="error", m="This is bad!": mb.showerror(t, m)).pack( fill="x", expand=True) tix.Button( f2, text="info", bg="pink", command=lambda t="info", m="Information": mb.showinfo(t, m)).pack( fill="x", expand=True) tix.Button(f2, text="warning", bg="yellow", command=lambda t="warning", m="Don't do it!": mb.showwarning( t, m)).pack(fill="x", expand=True) tix.Button( f2, text="question", bg="green", command=lambda t="question", m="Will I?": mb.askquestion(t, m)).pack( fill="x", expand=True) tix.Button(f2, text="yes - no", bg="lightgrey", command=lambda t="yes - no", m="Are you sure?": mb.askyesno( t, m)).pack(fill="x", expand=True) tix.Button(f2, text="yes - no - cancel", bg="black", fg="white", command=lambda t="yes - not - cancel", m="Last chance...": mb. askyesnocancel(t, m)).pack(fill="x", expand=True) f2.pack(side="top", fill="x") top.mainloop()
def _startGUI(self): '''Ouvre une fenêtre graphique. Dans cette version ce n'est pas réellement une application Python, la fenêtre ne fonctionne qu'à travers un shell Python (ex: IDLE) ''' global mode global GUI self.__gui = tix.Tk() self.__gui.title("HumaSudo - Sudoku humain") self.__ftop = tix.Frame(self.__gui) self.__st = tix.ScrolledText(self.__ftop, width=300, height=500) self.__st.pack(side="top", fill="both") self.__ftop.pack(side="top", fill="both") self.__disp = self.__st.subwidget("text") return
def _drawUI(self, frame): '''Dessine les widgets d'interface dans la frame indiquée.''' #canvas et dessin de la grille self._fgrid = tix.Frame(frame, bg="white") self._cnv = tix.Canvas(self._fgrid, width=280, height=280, bg="white") self._grid = SudoGuiGrid(self._cnv) #la grille est en haut de sa frame et de taille fixe self._cnv.pack(side="top") #texte déroulant self._ftext = tix.Frame(frame, bg="white") self._st = tix.ScrolledText(self._ftext, width=500, height=600) self._disp = self._st.subwidget("text") #la textbox est en haut de sa frame et la remplit self._st.pack(side="top", fill="both", expand=True) #arrangement des frames self._fgrid.pack(side="left", fill="y") self._ftext.pack(side="right", fill="both", expand=True) return
def __init__(self, parent, entries=None, title=None): tix.Frame.__init__(self, parent, title) self.entries = entries self.pathmap = {} # (path in listbox) -> (element of entries) self.pwindow = tix.PanedWindow(self, orientation='horizontal') self.p1 = self.pwindow.add('p1', at=0, expand=0) self.p2 = self.pwindow.add('p2', at=1, expand=1) # create scrolled listbox listframe = tix.Frame(self.p1, bd=2, relief=tix.SUNKEN) self.listbox = tix.Tree(listframe, browsecmd=self._browsecommand) self.listbox.hlist.config(width=40) self.listbox.pack(expand=True, fill=tix.BOTH) # self.listbox.insert(tix.END, *[a['name'] for a in self.entries]) # self.listbox.bind('<<ListboxSelect>>', self._lbselect) listframe.grid_rowconfigure(0, weight=1) listframe.grid_columnconfigure(0, weight=1) listframe.grid(row=0, column=0, padx=(0,5), sticky=tix.N+tix.S+tix.E+tix.W) self.p1.rowconfigure(0,weight=1) self.p1.columnconfigure(0,weight=1) # for some version of tix, auto option does not work but acts like 'both' self.summary = tix.ScrolledText(self.p2, bd=2, scrollbar='auto', relief=tix.SUNKEN) self.summary.text.config(width=80) self.summary.text.config(wrap=tix.NONE) self.summary.grid(row=0, column=0, padx=(5,0), sticky=tix.N+tix.S+tix.E+tix.W) self.p2.rowconfigure(0,weight=1) self.p2.columnconfigure(0,weight=1) self.pwindow.grid(row=0, column=0, sticky=tix.N+tix.S+tix.E+tix.W) self.rowconfigure(0, weight=1) self.columnconfigure(0, weight=1) self.set_listbox_entries(self.entries)
# create the event handler to clear the text def evClear(): txt = stHistory.subwidget('text') txt.insert('end',eHello.get()+'\n') eHello.delete(0, 'end') # create the top level window/frame top = tk.Tk() F = tk.Frame(top) F.pack(fill="both") # Now the frame with text entry fEntry = tk.Frame(F, border=1) eHello = tk.Entry(fEntry) eHello.pack(side="left") stHistory = tk.ScrolledText(fEntry, width=150, height=55) stHistory.pack(side="bottom", fill="x") fEntry.pack(side="top") # Finally the frame with the buttons. # We'll sink this one for emphasis fButtons = tk.Frame(F, relief="sunken", border=1) bClear = tk.Button(fButtons, text="Clear Text", command=evClear) bClear.pack(side="left", padx=5, pady=2) bQuit = tk.Button(fButtons, text="Quit", command=F.quit) bQuit.pack(side="left", padx=5, pady=2) fButtons.pack(side="top", fill="x") # Now run the eventloop F.mainloop()
import tkinter.tix as tix import tkinter.messagebox as mb top = tix.Tk() nb = tix.NoteBook(top, width=300, height=200) nb.pack(expand=True, fill='both') nb.add('page1', label="Text") f1 = tix.Frame(nb.subwidget('page1')) st = tix.ScrolledText(f1) st.subwidget('text').insert("1.0", "Here is where the text goes...") st.pack(expand=True) nb.add('page2', label="Message Boxes") f2 = tix.Frame(nb.subwidget('page2')) tix.Button( f2, text="error", bg="lightblue", command=lambda t="error", m="This is bad!": mb.showerror(t, m)).pack( fill='x', expand=True) tix.Button(f2, text="info", bg='pink', command=lambda t="info", m="Information": mb.showinfo(t, m)).pack( fill='x', expand=True) tix.Button( f2, text="warning", bg='yellow',
# Notebook widget example using tix import tkinter.tix as tix import tkinter.messagebox as mb top = tix.Tk() nb = tix.NoteBook(top, width=300, height=300) nb.pack(expand=True, fill="both") nb.add("Page1", label="Text") frame1 = tix.Frame(nb.subwidget("page1")) st = tix.ScrolledText(frame1) st.subwidget("text").insert("1.0", "Type text here...") st.pack(expand=True) frame1.pack() nb.add("Page2", label="Message boxes") frame2 = tix.Frame(nb.subwidget("page2")) tix.Button(frame2, text="Error", bg="red", command=lambda t="error", m="This is bad": mb.showerror(t, m)).pack( fill="x", expand=True) tix.Button( frame2, text="Info", bg="lightblue", command=lambda t="info", m="Some Information": mb.showinfo(t, m)).pack( fill="x", expand=True)