【问题标题】:How do I make a button console using Tkinter?如何使用 Tkinter 制作按钮控制台?
【发布时间】:2015-04-30 14:09:19
【问题描述】:

我有一个文本菜单中有九个不同选项的脚本。我想使用 tkinter 更改 GUI 的菜单。

菜单有九个选项,分别是从 1 到 9 的 bucle if, elif... esle。最后一个是“exit”选项。

如果,elif,elif....else 在一个有九个按钮的窗口中,我如何转换菜单,每个按钮用于不同的选项并运行相同的脚本?

我正在尝试以下代码:

from tkinter import*
ventana = Tk()
variable = '' 
def opcion1():
    global variable
    variable = '1'def opcion2 ():
global variable
variable = '2'
root = Tk()
boton1 = Button(ventana, text='OPCION1',command=opcion1)
boton1.pack()

boton2 = Button(ventana, text='OPCION2',command=opcion2)
boton2.pack()

botonSalir = Button(ventana, text='EXIT',command=quit)
botonSalir.pack()
root.mainloop()

我该怎么做?

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    希望这会有所帮助!

    from Tkinter import *
    root = Tk()
    def f1():
        print('f1')
    def f2():
        print('f2')
    def f3():
        print('f3')
    
    MODES = [("Option1", f1, '1'), ("Option2", f2, '2'), ("Option3", f3, '3')]
    
    v = StringVar()
    v.set("L") # initialize
    
    for text, function, mode in MODES:
        b = Radiobutton(root, text=text, indicatoron=0, variable=v, command=function, value=mode)
        b.pack(anchor=W)
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2014-02-13
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2015-09-16
      • 1970-01-01
      • 2018-07-24
      相关资源
      最近更新 更多