Python GUI回调函数

时间:2018-08-11 20:08:14

标签: python function user-interface tkinter

我正在编写一个Python程序,该程序使用GUI计算每加仑英里数(MPG)。 下面的代码包含一个名为calc_mpg的函数,该函数用于使用主函数“ miles_entry”条目和“ gallons_entry”条目的输入来计算单击“ calc_button”时每加仑汽车的英里数。

单击按钮时,出现一个错误,提示未定义'miles_entry'。有没有一种方法可以重组或重新编写此代码以按预期运行。

import tkinter
import tkinter.messagebox

def main():
     main_window = tkinter.Tk()
     upper_frame = tkinter.Frame(main_window)
     lower_frame = tkinter.Frame(main_window)

     label1 = tkinter.Label(upper_frame, text='This program calculates MPG')

     miles_prompt = tkinter.Label(upper_frame, text='How many miles can be 
     driven with full gastank?')
     miles_entry = tkinter.Entry(upper_frame, width = 10)
     gallons_prompt = tkinter.Label(upper_frame, text='How many gallons of 
     gas can the tank hold?')
     gallons_entry = tkinter.Entry(upper_frame, width = 10)

     miles_prompt.pack()
     miles_entry.pack()
     gallons_prompt.pack()
     gallons_entry.pack()

     calc_button = tkinter.Button(lower_frame, text='Calculate MPG', 
     command=calc_mpg)
     quit_button = tkinter.Button(lower_frame, text='Quit', 
     command=main_window.destroy)



    upper_frame.pack()
    lower_frame.pack()
    calc_button.pack()
    quit_button.pack()
    tkinter.mainloop()


def calc_mpg():
    miles = float(miles_entry.get())
    gallons = float(gallons_entry.get())

    mpg = miles / gallons

    tkinter.messagebox.showinfo('The miles per gallon for this vehicle is:' 
   + mpg + 'mpg.') 



main()

2 个答案:

答案 0 :(得分:1)

您为为什么通常将import tkinter import tkinter.messagebox class main: def __init__(self): main_window = tkinter.Tk() upper_frame = tkinter.Frame(main_window) lower_frame = tkinter.Frame(main_window) label1 = tkinter.Label(upper_frame, text='This program calculates MPG') miles_prompt = tkinter.Label(upper_frame, text='How many miles can be driven with full gastank?') self.miles_entry = tkinter.Entry(upper_frame, width = 10) gallons_prompt = tkinter.Label(upper_frame, text='How many gallons of gas can the tank hold?') self.gallons_entry = tkinter.Entry(upper_frame, width = 10) miles_prompt.pack() self.miles_entry.pack() gallons_prompt.pack() self.gallons_entry.pack() calc_button = tkinter.Button(lower_frame, text='Calculate MPG', command=self.calc_mpg) quit_button = tkinter.Button(lower_frame, text='Quit', command=main_window.destroy) upper_frame.pack() lower_frame.pack() calc_button.pack() quit_button.pack() tkinter.mainloop() def calc_mpg(self): miles = float(self.miles_entry.get()) gallons = float(self.gallons_entry.get()) mpg = miles / gallons tkinter.messagebox.showinfo(title="Some info!",message='The miles per gallon for this vehicle is:' + str(mpg) + 'mpg.') main() 应用程序包装在一个类中而感到恼火。通过使用类,您可以持久保存对象并在方法之间使用它们:

self

我添加了mpg来引用这两个条目,因此您可以在其他方法中使用它们(默认情况下,变量仅存在于定义它们的函数中)。另请注意:

  1. 您忘记了将self转换回信息框中的字符串
  2. 信息框的第一个参数是标题,而不是内容

如果对tk表示法或类不熟悉,则应在创建fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLFVN3H0G8MT", Request id "0HLFVN3H0G8MT:00000001": An unhandled exception was thrown by the application. Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred: jhhodq42.4nm(4,41): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?) jhhodq42.4nm(5,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. 应用程序之前先进行研究。

答案 1 :(得分:0)

在main()范围内的miles_entry和gallon_entry在calc_mpg()中不可见。 @kabanus是正确的。 另一种工作方式是使用functools模块中的partial()。可以将参数传递给回调:

from functools import partial
...
calc_button = tkinter.Button(lower_frame, text='Calculate MPG', command=partial(calc_mpg,miles_entry,gallons_entry) )
...
def calc_mpg(miles_entry,gallons_entry):
  ...