gpt4 book ai didi

python tkinter 组合框/按钮

转载 作者:太空宇宙 更新时间:2023-11-03 18:46:53 24 4
gpt4 key购买 nike

我刚刚开始使用 python 和 tkinter,我创建了一个带有两个组合框和“运行”按钮的 gui,其中包含两个不同的选项“SPMI”和“RFFE”。我希望我的脚本做的是,当我选择不同的选项并单击“运行”时,它应该运行 SPMI.py 或 RFFE.py 文件。

请帮忙,谢谢

我的代码:

import sys
from Tkinter import *



def callback1():
os.system('SPMI.py')

def callback2():
os.system('RFFE.py')

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')

b1_1 = Button(Dragonfly, text = "Run", fg='blue',command=callback1)
b1_1.place(x=85,y=150)


b2_2= Button(Dragonfly, text = "Run", fg='blue',command=callback2)
b2_2.place(x=275,y=150)


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')
mymenu2 = MyOptionMenu(Dragonfly, 'Select Protocol', 'SPMI','RFFE')
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()

最佳答案

使用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()
<小时/>

顺便说一句,使用 place 构建 GUI 一开始很容易,但最终却很麻烦。一方面,随着它的增长,更改布局变得越来越困难,因为插入新的小部件往往需要摆弄整个地方的硬编码坐标。另一个问题是小部件布局无法适应窗口大小的变化。

出于这些原因,人们倾向于使用 packgrid 来布局小部件:

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()

关于python tkinter 组合框/按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19324007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com