1

使用 Python 2.7.3,我创建了以下 tkinter 代码。该代码要求用户在 GUI 中输入两个值,提交并关闭 GUI。但是我遇到了提交按钮的问题。当用户单击提交时,会发生以下情况: 1. 一个 if 语句来确认 Value 1 是否有值,如果没有,则会出现一个消息框。2. if 语句确认 Value 2 是否有值,如果没有,则出现消息框。3. 如果值 1 和值 2 都有值,则 GUI 将关闭。

然而,这是我遇到问题的地方,我进行了以下测试: 1. 为值 2 输入了一个值,而为值 1 输入了一个值,第一个 if 语句被触发,这很好。2. 输入值 1 的值,而值 2 没有值,触发了第二个 if 语句,这很好。3. 输入值 1 和值 2 的值,导致 GUI 关闭,这很好,但 python 代码挂起,最后一行代码“print c”没有出现。

这是什么原因造成的?

这个想法是这个 GUI 将用于输入,用于我的函数文件。此代码是对 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
4

1 回答 1

0
  • 固定缩进。
  • 删除了 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
于 2013-06-13T16:49:11.763 回答