用于self.var.get()
获取组合框的值。然后,您可以在回调中使用该值来使回调的行为取决于组合框的设置。
import sys
from Tkinter import *
import subprocess
class MyOptionMenu(OptionMenu):
def __init__(self, master, status, *options):
self.var = StringVar(master)
self.var.set(status)
OptionMenu.__init__(self, master, self.var, *options)
self.config(
font=('calibri', (10)), bg='white', width=12, fg='dark red')
self['menu'].config(font=('calibri', (10)), bg='white', fg='dark blue')
def callback(self):
val = '{}.py'.format(self.var.get())
print(val)
# subprocess.call([val])
Dragonfly = Tk()
Dragonfly.geometry('400x400+400+300')
Dragonfly.title('Dragonfly')
mainlabel = Label(text='Dragonfly Trigger Test', font=('calibri', (14)),
fg='dark blue').pack()
mymenu1 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
b1_1 = Button(Dragonfly, text="Run", fg='blue', command=mymenu1.callback)
b1_1.place(x=85, y=150)
mymenu2 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
b2_2 = Button(Dragonfly, text="Run", fg='blue', command=mymenu2.callback)
b2_2.place(x=275, y=150)
mymenu1.place(x=40, y=100)
mymenu2.place(x=230, y=100)
m1label = Label(text='Frame Trigger', font=('calibri', (12)),
fg='dark green').place(x=57, y=60)
m1labe2 = Label(text='External Trigger', font=('calibri', (12)),
fg='dark green').place(x=240, y=60)
Dragonfly.mainloop()
顺便说一句,用 构建一个 GUIplace
起初很容易,但最终却很麻烦。一方面,随着它的增长,更改布局变得越来越难,因为插入一个新的小部件往往需要摆弄整个地方的硬编码坐标。另一个问题是小部件布局无法适应窗口大小的变化。
由于这些原因,人们倾向于使用pack
或grid
布局小部件:
import sys
import Tkinter as tk
import subprocess
class MyOptionMenu(tk.OptionMenu):
def __init__(self, master, status, *options):
self.var = tk.StringVar(master)
self.var.set(status)
tk.OptionMenu.__init__(self, master, self.var, *options)
self.config(
font=('calibri', (10)), bg='white', width=12, fg='dark red')
self['menu'].config(font=('calibri', (10)), bg='white', fg='dark blue')
def callback(self):
val = '{}.py'.format(self.var.get())
print(val)
# subprocess.call([val])
Dragonfly = tk.Tk()
Dragonfly.geometry('400x400+400+300')
Dragonfly.title('Dragonfly')
Dragonfly.columnconfigure(0, weight=1)
Dragonfly.columnconfigure(1, weight=1)
mainlabel = tk.Label(text='Dragonfly Trigger Test', font=('calibri', (14)),
fg='dark blue').grid(row=0, column=0, columnspan=2, pady=20)
mymenu1 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
mymenu1.grid(row=2, column=0, pady=10, padx=10,)
b1_1 = tk.Button(Dragonfly, text="Run", fg='blue', command=mymenu1.callback)
b1_1.grid(row=3, column=0, pady=10, padx=10,)
mymenu2 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI', 'RFFE')
mymenu2.grid(row=2, column=1, pady=10, padx=10,)
b2_2 = tk.Button(Dragonfly, text="Run", fg='blue', command=mymenu2.callback)
b2_2.grid(row=3, column=1, pady=10, padx=10,)
m1label = tk.Label(text='Frame Trigger', font=('calibri', (12)),
fg='dark green').grid(row=1, column=0, pady=10, padx=10,)
m1labe2 = tk.Label(text='External Trigger', font=('calibri', (12)),
fg='dark green').grid(row=1, column=1, pady=10, padx=10,)
Dragonfly.mainloop()