Вопрос

Using Python 2.7.3, I created the following tkinter code. The code requires the user to input two values into the GUI, which are submitted and the GUI is closed. However I am having problems with the submit button. When the user clicks submit the following occurs: 1. An if statement to confirm if Value 1 has a value, if not a messagebox appears. 2. An if statement to confirm if Value 2 has a value, if not a messagebox appears. 3. If both Value 1 and Value 2 have a value then the GUI will be closed.

However this is where I am having problems, I carried out the following tests: 1. Entered a value for Value 2 and no value for Value 1, the first if statement was triggered, this was fine. 2. Entered a value for Value 1 and no value for Value 2, the second if statement was triggered, this was fine. 3. Entered valuesfor Value 1 and Value 2, which caused the GUI to close, which was fine but the python code hangs and the last line of code "print c" didn't occur.

What is causing this?

The idea is that this GUI will be used for inputs, for my function file. This code is a test of the GUI.

import Tkinter
import sys
import tkMessageBox


class GUI(Tkinter.Tk):
"""docstring for Values"""
   def __init__(self, parent):
       Tkinter.Tk.__init__(self,parent)
       self.parent = parent

       ###if user hits close button
       def callback():
            if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
            self.destroy()
            sys.exit()

       self.protocol("WM_DELETE_WINDOW", callback) 
       self.initialize()

  def initialize(self):
      self.grid()
      stepOne = Tkinter.LabelFrame(self, text=" 1. Enter Values ")
      stepOne.grid(row=0, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
      self.Val1Lbl = Tkinter.Label(stepOne,text="Value 1")
      self.Val1Lbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
      self.Val1Txt = Tkinter.Entry(stepOne)
      self.Val1Txt.grid(row=0, column=1, columnspan=3, pady=2, sticky='WE')
      self.Val2Lbl = Tkinter.Label(stepOne,text="Value 2")
      self.Val2Lbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
      self.Val2Txt = Tkinter.Entry(stepOne)
      self.Val2Txt.grid(row=1, column=1, columnspan=3, pady=2, sticky='WE')

      self.val1 = None
      self.val2 = None

      self.SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=self.submit)
      self.SubmitBtn.grid(row=4, column=3, sticky='W', padx=5, pady=2)

  def submit(self):
      self.val1=self.Val1Txt.get()
      if self.val1=="":
         Win2=Tkinter.Tk()
         Win2.withdraw()
         tkMessageBox.showinfo(message="Value 1 has no values entered")

      self.val2=self.Val2Txt.get()
      if self.val2=="":
         Win2=Tkinter.Tk()
         Win2.withdraw()
         tkMessageBox.showinfo(message="Value 2 has no values entered")

    ###Close GUI if Val1 and Val2 have values    
      if len(self.val2)>0 and len(self.val1)>0:
         self.destroy()


app = GUI(None)
app.title('Values')
app.mainloop()
#calculate values of Val1 and Val2 
a=float(app.val1)
b=float(app.val2)
c=a+b
print c
Это было полезно?

Решение

  • Fixed indentation.
  • Removed Tkinter.Tk()

import Tkinter
import sys
import tkMessageBox


class GUI(Tkinter.Tk):
    """docstring for Values"""
    def __init__(self, parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent

        ###if user hits close button
        def callback():
            if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
                self.destroy()
                sys.exit()

        self.protocol("WM_DELETE_WINDOW", callback)
        self.initialize()

    def initialize(self):
        self.grid()
        stepOne = Tkinter.LabelFrame(self, text=" 1. Enter Values ")
        stepOne.grid(row=0, columnspan=7, sticky='W',padx=5, pady=5, ipadx=5, ipady=5)
        self.Val1Lbl = Tkinter.Label(stepOne,text="Value 1")
        self.Val1Lbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
        self.Val1Txt = Tkinter.Entry(stepOne)
        self.Val1Txt.grid(row=0, column=1, columnspan=3, pady=2, sticky='WE')
        self.Val2Lbl = Tkinter.Label(stepOne,text="Value 2")
        self.Val2Lbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
        self.Val2Txt = Tkinter.Entry(stepOne)
        self.Val2Txt.grid(row=1, column=1, columnspan=3, pady=2, sticky='WE')

        self.val1 = None
        self.val2 = None

        self.SubmitBtn = Tkinter.Button(stepOne, text="Submit",command=self.submit)
        self.SubmitBtn.grid(row=4, column=3, sticky='W', padx=5, pady=2)

    def submit(self):
        self.val1 = self.Val1Txt.get()
        if self.val1 == "":
             tkMessageBox.showinfo(message="Value 1 has no values entered")
             return

        self.val2 = self.Val2Txt.get()
        if self.val2 == "":
             tkMessageBox.showinfo(message="Value 2 has no values entered")
             return

        ###Close GUI if Val1 and Val2 have values
        if self.val1 and self.val2:
             self.destroy()


app = GUI(None)
app.title('Values')
app.mainloop()
#calculate values of Val1 and Val2
a=float(app.val1)
b=float(app.val2)
c=a+b
print c
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top