Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python GUI单位转换米/英尺_Python_User Interface_Tkinter_Computer Science_Metrics - Fatal编程技术网

Python GUI单位转换米/英尺

Python GUI单位转换米/英尺,python,user-interface,tkinter,computer-science,metrics,Python,User Interface,Tkinter,Computer Science,Metrics,我已经在Tkinter模块上工作了几个星期,现在开始编写GUI。我有一个问题,当我按下计算按钮,它没有显示任何东西。它的目标是在用户选择米或用户选择英尺转换为米时显示25米到英尺的转换。我希望它能像这段视频一样工作。任何帮助都将不胜感激 以下内容基本上满足了您的需求。我重新命名了您的StringVars,并添加了一个附加值来保存结果 rom tkinter import * from tkinter import ttk def calculate(*args): if choices

我已经在Tkinter模块上工作了几个星期,现在开始编写GUI。我有一个问题,当我按下计算按钮,它没有显示任何东西。它的目标是在用户选择米或用户选择英尺转换为米时显示25米到英尺的转换。我希望它能像这段视频一样工作。任何帮助都将不胜感激


以下内容基本上满足了您的需求。我重新命名了您的
StringVar
s,并添加了一个附加值来保存结果

rom tkinter import *
from tkinter import ttk

def calculate(*args):
    if choices.get() == 'meters':
        value = float(inp.get())
        result.set(int(3.281 * value * 10000.0 + 0.5) / 10000.0)
        units.set('feet')
    if choices.get() == 'feet':
        value = float(inp.get())
        result.set(int(0.3048 * value * 10000.0 + 0.5) / 10000.0)
        units.set('meters')

root = Tk()
root.title('Length Converter')

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
mainframe = ttk.Frame(root, padding='25 25 25 25')
mainframe.grid(sticky=(N, W, E, S))

inp = StringVar()
choice = StringVar()
result = StringVar()
units = StringVar()

input_entry = ttk.Entry(mainframe, width=7, textvariable=inp)
input_entry.grid(row=1, column=0)
choices = ttk.Combobox(mainframe, width=10, textvariable=choice, values=('meters', 'feet'))
choices.grid(row=1, column=1)

ttk.Label(mainframe, text='is equal to').grid(row=2, column=0)
ttk.Label(mainframe, textvariable=result).grid(row=2, column=1, sticky=E)
ttk.Label(mainframe, textvariable=units).grid(row=2, column=2, sticky=W)

ttk.Button(mainframe, text='Calculate', command=calculate).grid(row=3, column=1)

for child in mainframe.winfo_children():
    child.grid_configure(padx=5, pady=5)

root.mainloop()

当从米转换为英尺时,代码会更新名为
feet
StringVar
,但该值不会显示在GUI中的任何位置。
rom tkinter import *
from tkinter import ttk

def calculate(*args):
    if choices.get() == 'meters':
        value = float(inp.get())
        result.set(int(3.281 * value * 10000.0 + 0.5) / 10000.0)
        units.set('feet')
    if choices.get() == 'feet':
        value = float(inp.get())
        result.set(int(0.3048 * value * 10000.0 + 0.5) / 10000.0)
        units.set('meters')

root = Tk()
root.title('Length Converter')

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
mainframe = ttk.Frame(root, padding='25 25 25 25')
mainframe.grid(sticky=(N, W, E, S))

inp = StringVar()
choice = StringVar()
result = StringVar()
units = StringVar()

input_entry = ttk.Entry(mainframe, width=7, textvariable=inp)
input_entry.grid(row=1, column=0)
choices = ttk.Combobox(mainframe, width=10, textvariable=choice, values=('meters', 'feet'))
choices.grid(row=1, column=1)

ttk.Label(mainframe, text='is equal to').grid(row=2, column=0)
ttk.Label(mainframe, textvariable=result).grid(row=2, column=1, sticky=E)
ttk.Label(mainframe, textvariable=units).grid(row=2, column=2, sticky=W)

ttk.Button(mainframe, text='Calculate', command=calculate).grid(row=3, column=1)

for child in mainframe.winfo_children():
    child.grid_configure(padx=5, pady=5)

root.mainloop()