Python实现GUI菜单背景显示文字用什么方法,pythongui,我现在有一个Python


我现在有一个Python写的用Tkinter做出来的GUI式的菜单,作为我的用户界面。每当用户手动用鼠标点击这个菜单上的某些按键时,我希望都能触发一些其他的Python函数。代码如下:

from Tkinter import Frame, Tk, BOTH, Text, Menu, END, Label, SUNKEN, X, TOP, BOTTOMimport tkFileDialogfrom ttk import Frame as Frames, Button, Styleclass Example(Frame):    def __init__(self, parent, number):        Frame.__init__(self, parent)        self.parent = parent        self.number = number        self.initUI()    def initUI(self):        self.parent.title("Radiation Simulation")        self.pack(fill=BOTH, expand=1)        menubar = Menu(self.parent)        self.parent.config(menu=menubar)        file_menu = Menu(menubar)        file_menu.add_command(label="Open", command=self.hello)        file_menu.add_command(label="Quit", command=self.quit)        menubar.add_cascade(label="File", menu=file_menu)    def hello(self):        print "Hello, everyone!"    def statusBarUpdate(self, event=None):        print "Status Bar Update Called"        if self.parent.call(event.widget, "index", "active") == 0:            self.statusBar.display("About This Application")        else:            self.statusBar.display("")class StatusBar(Frame):    def __init__(self, master):        Frame.__init__(self, master)        self.label = Label(self, bd = 1, text="This is the initialization!", font=10, relief = SUNKEN, anchor = "w")        self.label.pack(fill=X)    def display(self, format0, *args):        self.label.config(text = format0 % args)        self.label.update_idletasks()    def clear(self):        self.label.config(text="")        self.label.update_idletasks()def Dialog_main(string):    root = Tk()    ex = Example(root, string)    root.geometry("800x600+600+400")    root.mainloop()def Dialog_other()    # ... Generates some texts. Details omitted...    return textsif __name__ == '__main__':    Dialog_other()    Dialog_main(texts)

这个程序主要就是有两个函数:Dialog_main和Dialog_other。Dialog_other是一个比较复杂的函数,我就略去其内容了,总之它output是一个text。Dialog_main是一个专门的GUI函数,input就是Dialog_other的text。运行之后,GUI式的菜单就会生成,菜单中会有一些按键:

我希望Dialog_main能够顺利从Dialog_other那里接收这个text,然后把它作为背景的文本显示在菜单背景上。

请问有人能帮我看看这两个功能该如何实现么?我目前的代码该怎样更改?

谢谢了先!

你说的背景是指菜单文字?可以试试variabletext试试,不确定是否在menu上可用。
可以确定的是,如果你需要文字替换,label/entry是一定行的!
self.label_ip = Label(self, text=u"Ip address goes here.", anchor=E, justify=LEFT, textvariable=self.v_ip)
btw:
tkinter是包装在tk上的,不要希望有很炫的效果,够用就行了。

编橙之家文章,

评论关闭