2015-04-18 122 views
0

我一直在試圖做一個簡單的計算器,但我主要在兩件事上掙扎。tkinter給出結果的單獨窗口

  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() 
+0

「他們都不工作」太含糊。 –

回答

0

對於單獨的窗口,使用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,並且不要忘記在字符之間添加空格。

+0

感謝您回覆隊友,這個建議很有效,但現在我又遇到了另一個問題,那就是如何評估輸入輸入並只接受數字,並且顯示用戶輸入的消息並不值錢。我正在嘗試使用validatecommand,但它不起作用。不幸的是,tkinter中的東西是不同的。 –

+0

@Janomannan:很簡單。替換新響應的操作功能。 – DCPY

+0

你能寫一個簡短的代碼嗎? –