Python GUI程序,两次转换

时间:2017-05-17 03:06:04

标签: python user-interface tkinter

我正在设计一个带有2个转换窗口的程序。一公里到几公里,另一公里到公里。 GUI消息框有两个要键入的位置,但只有kilomiles框给我结果。我尝试了不同的东西,但我还没有弄清楚如何获得它,所以无论我输入哪个盒子,转换按钮都能正常工作。

import tkinter
import tkinter.messagebox


class MyGUI:
    def __init__(self):

    # Create the main window widget.
    self.main_window = tkinter.Tk()

    # Create two frames
    self.top_frame = tkinter.Frame(self.main_window)
    self.bottom_frame = tkinter.Frame(self.main_window)

    # Create three Label widgets for the
    # top frame.
    self.label1 = tkinter.Label(self.top_frame, \
                                text='Lets Have Fun')
    self.label2 = tkinter.Label(self.top_frame, \
                                text='Calculate Distance')
    self.prompt_label = tkinter.Label(self.top_frame, \
                text='Enter how many kilometers you drove today:')
    self.kilo_entry = tkinter.Entry(self.top_frame, \
                                    width=10)
    self.prompt_label2 = tkinter.Label(self.top_frame, \
                text='Enter how many miles you drove today:')
    self.miles_entry = tkinter.Entry(self.top_frame, \
                                    width=10)
    # Pack the labels that are in the top frame.
    # Use the side='top', 'left' and 'bottom' arguments to stack them
    # one on top of the other.

    self.label1.pack(side='top')
    self.label2.pack(side='top')
    self.prompt_label.pack(side='left')
    self.kilo_entry.pack(side='left')
    self.prompt_label2.pack(side='left')
    self.miles_entry.pack(side='left')


    self.top_frame.pack()
    self.bottom_frame.pack()


    self.my_button = tkinter.Button(self.main_window, \
                                    text='What if I dont know?', \
                                    command=self.do_something)
    self.calc_button = tkinter.Button(self.top_frame, \
                                     text='Convert', \
                                     command=self.convert)
    self.quit_button = tkinter.Button(self.main_window, \
                                      text='Quit', \
                                      command=self.main_window.destroy)

    self.my_button.pack()
    self.quit_button.pack()
    self.calc_button.pack()

    tkinter.mainloop()

def do_something(self):
    tkinter.messagebox.showinfo('Response', \
                                'Just enter your favorite number.')

miles = 0.00
miles == " "




def convert(self):
    # Get the value entered by the user into the
    # kilo_entry widget.
    kilo = float(self.kilo_entry.get())

    # Convert kilometers to miles.
    miles = kilo * 0.6214

    # Display the results in an info dialog box.
    tkinter.messagebox.showinfo('Results', \
        str(kilo) + ' kilometers is equal to ' + \
        str(miles) + ' miles.')
    def convert2(self):
    if self.kilo_entry == " ":
       convert2(self)
    else:
       convert(self)


    tkinter.mainloop()


    # Get the value entered by the user into the
    # kilo_entry widget.
    miles = float(self.miles_entry.get())

    # Convert kilometers to miles.
    kilo = (miles) / (0.6214)

    # Display the results in an info dialog box.
    tkinter.messagebox.showinfo('Results', \
        str(miles) + ' miles is equal to ' + \
        str(kilo) + ' kilo.')

my_gui = MyGUI()

谢谢!

1 个答案:

答案 0 :(得分:0)

def do_something(self)之后的代码有点笨拙。特别是,您不应多次拨打mainloop()

实现convert()方法的一种方法是检查输入框是否为空,如果不是,则转换其内容,如下所示:

class MyGUI:
    def __init__(self):
        # Create the main window
        self.main_window = tkinter.Tk()

        # Define all widgets
        ...

        # Launch the app
        self.main_window.mainloop()

    def do_something(self):
        tkinter.messagebox.showinfo('Response', \
                                    'Just enter your favorite number.')    

    def convert(self):
        # Convert each box if not empty
        if self.kilo_entry.get() != "":
            self.convert_kilo2miles()
        if self.miles_entry.get() != "":
            self.convert_miles2kilo()

    def convert_kilo2miles(self):
        # Get the value entered by the user into the kilo_entry widget.
        kilo = float(self.kilo_entry.get())

        # Convert kilometers to miles.
        miles = kilo * 0.6214

        # Display the results in an info dialog box.
        tkinter.messagebox.showinfo('Results', \
            str(kilo) + ' kilometers is equal to ' + \
            "{:.3f}".format(miles) + ' miles.')

    def convert_miles2kilo(self):
        # Get the value entered by the user into the miles_entry widget.
        miles = float(self.miles_entry.get())

        # Convert miles to km.
        kilo = (miles) / (0.6214)

        # Display the results in an info dialog box.
        tkinter.messagebox.showinfo('Results', \
            str(miles) + ' miles is equal to ' + \
            "{:.3f}".format(kilo) + ' kilometers.')