【问题标题】:separate window in tkinter giving resulttkinter 中的单独窗口给出结果
【发布时间】:2015-04-18 01:28:18
【问题描述】:

我一直在尝试制作一个简单的计算器,但我主要在两件事上苦苦挣扎。

  1. 在 tkinter 中创建一个执行算术方程的函数
  2. 在单独的窗口中显示结果

我尝试使用顶级小部件和 showMessagebox 在新窗口中显示结果,但它们都不起作用!

代码:

from Tkinter import *
import math
import tkMessageBox

class Calculator():

     def __init__(self,master):

          self.master = master
          self.master.configure(background='sky blue')
          self.master.geometry('650x420+350+225')

          self.master.title('Calculator')

          self.ini_velocity = DoubleVar()

          # here we start creating buttons and entry boxes
          self.m_label = Label(text='Calculator',fg = 'Navy', font=("Helvetica", 20,"bold italic"), bg='sky blue')
          self.m_label.pack()

          self.button1 = Button(self.master,text='Final velocity',fg='white',bg='dark green',bd =3, width=12, command= self.show_m)
          self.button1.place(x=52,y=155)

          self.label1=Label(self.master,text='''1. v = u + a*t

     Initial Velocity

     Acceleration

          Time ''', fg= 'Navy', font='Helvetica 10 bold',bg='sky blue')
          self.label1.place(x=0,y=30)

          self.e1= Entry(self.master, textvariable = self.ini_velocity, width=4,bd=2)
          self.e1.place(x=120, y=62)

          self.e2= Entry(self.master, width=4, bd=2)
          self.e2.place(x=120, y=92)

          self.e3=Entry(self.master, width=4,bd=2)
          self.e3.place(x=120, y=122)

     def my_calculation(self): # this function is to operate the calculation
          root2 = Toplevel(self.master)
          myGUI = result_window(root2)

     def my_quit(self):
          self.master.destroy()

     def myresault(self):
          self.a = self.ini_velocity.get()


class result_window ():
     def __init__(self,master):
          self.master = master
          self.master.configure(background='sky blue')
          self.master.geometry('250x175+150+125')
          self.master.title('resault')

          print (Calculator.self.e1.get())

     def F_velocity(self):
          ini_v = self.ini_velocity.get()
          print (ini_v)


          # end of button commands
def main():

     root = Tk()
     myGUIcalculator = Calculator(root)
     root.mainloop()

if __name__=='__main__':
     main()

【问题讨论】:

  • “它们都不起作用”太模糊了。

标签: python-2.7 tkinter


【解决方案1】:

对于单独的窗口,使用tkMessageBox(如您所想)。使用tkMessageBox.showinfo(title, message)。示例:

from Tkinter import *
import tkMessageBox

numbersandoperatorslist = "1 2 3 4 5 6 7 8 9 0 - + = / *".split(" ")

def operation(content):
    contentlist = []
    for char in range(len(content)):
        contentlist.append(char) # This might be useful later
        if char not in numbersandoperatorslist:
            tkMessageBox.showerror("Error", "Your operation has an undefined character.")
            return
    result = eval(content)
    tkMessageBox.showinfo("Result", str(result))

content 是您的操作。如果要添加/删除可接受的字符,只需编辑numbersandoperatorslist,不要忘记在字符之间添加空格。

【讨论】:

  • 感谢您的回复,这个建议有效,但现在我遇到了另一个问题,即如何评估输入输入并仅接受数字并为用户显示输入的消息没有价值。我正在尝试使用 validatecommand 但它不起作用。不幸的是,tkinter 中的情况有所不同。
  • @Janomannan:这很简单。替换新响应的操作函数。
  • @Jannomannan:对不起。我只是花了一点时间。 response 不是正确的词;应该是answer
  • 非常感谢您的努力,由于代码中的一些算术运算,我不知道如何安排您的代码。我找到了另一种方法,但它部分有效。例子。
  • @Jannomannan:什么样的运营商?平方根之类的东西?然后将numbersandoperatorslist 设置为"1 2 3 4 5 6 7 8 9 0 + - = * / V".split(" ")。这和["1", '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '+', '*', '/', 'V']是一样的,在eval行前加上math.sqrt(替换任何​​'V'V value),然后继续eval,等
猜你喜欢
  • 2019-09-05
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 2018-10-31
  • 1970-01-01
  • 2020-10-15
相关资源
最近更新 更多