Skip to main content
 首页 » 编程设计

python之将命令分配给按钮之Tkinter

2024年10月17日6over140

我想找出必须将哪个命令分配给 Tkinter GUI 中的按钮才能打印结果。

设置是运行 aaaa.py。

def question(): 
    import xxxx 
    return xxxx.hoeveel() 
 
 
if __name__ == '__main__': 
    from bbbb import response 
    def juist(): 
        return response() 
    print juist() 

运行 aaaa.py 时,我得到一个基于脚本 xxxx.py 的 Tkinter GUI

from Tkinter import * 
import ttk 
 
 
 
def hoeveel(): 
    return int(x=gewicht.get(), base=10) 
 
frame = Tk() 
 
gewicht = StringVar() 
 
a = Label(frame, text="Wat is uw gewicht in kilogram?:").grid(row=1, column=1, sticky='w') 
aa = Entry(frame, text="value", textvariable=gewicht, justify='center', width=10) 
aa.grid(row=1, column=2, padx=15) 
 
bereken = ttk.Button(frame, text='Bereken') 
bereken.grid(column=1, row=2, columnspan=2, ipadx=15, pady=25) 
mainloop() 

Tkinter GUI xxxx.py 中给出的输入被发送到 bbbb.py 进行一些计算。

from aaaa import question 
mass_stone = question() * 2.2 / 14 
 
 
def response(): 
    return str("Uw gewicht in kilograms is gelijk aan " + ("{0:.5}".format(mass_stone)) + " stone.") 

我的问题是,当我关闭 Tkinter 窗口时,我只得到输出“Uw gewicht in kgs is gelijk aan "x (depending on the value input)"stone。

我想在按下按钮时得到结果。

有什么建议吗?

请您参考如下方法:

我想我已经找到了答案,但我不确定这是最优雅的方法。 如果您知道其他方式和更正确的方式,请告诉我们。

所以,你需要运行aaaa.py

import xxxx 
def question(): 
    return xxxx.hoeveel() 

xxxx.py

from Tkinter import * 
import ttk 
import bbbb 
 
def hoeveel(): 
    return int(x=gewicht.get(), base=10) 
 
frame = Tk() 
 
gewicht = StringVar() 
 
a = Label(frame, text="Wat is uw gewicht in kilogram?:").grid(row=1, column=1, sticky='w') 
aa = Entry(frame, text="value", textvariable=gewicht, justify='center', width=10) 
aa.grid(row=1, column=2, padx=15) 
 
bereken = ttk.Button(frame, text='Bereken', command=bbbb.berekening) 
bereken.grid(column=1, row=2, columnspan=2, ipadx=15, pady=25) 
mainloop() 

最后

bbbb.py

from aaaa import question 
def berekening(): 
    mass_stone = question() * 2.2 / 14 
    print mass_stone 

当你运行 aaaa.py 时,你会得到带有问题的 Tkinter Gui。 填写您的体重,然后按“Bereken” 你会得到我想要的打印出来的答案。