2014-05-24 80 views
0

我想用python製作一個基本的計算器,但是我用if語句有問題。下面是代碼:如果在pyGTK語句

import gtk 
class PyApp(gtk.Window): 
    def __init__(self): 
     super(PyApp,self).__init__() 
     self.set_title("Project") 
     self.set_size_request(245,400) 
     self.set_position(gtk.WIN_POS_CENTER) 
     fixed = gtk.Fixed() 
     self.label = gtk.Label("Welcome to my project!") 
     fixed.put(self.label, 60, 40) 
     self.lbl = gtk.Label("") 
     fixed.put(self.lbl,60,200) 
     entry = gtk.Entry() 
     entry2 = gtk.Entry() 
     self.char = gtk.Entry() 
     fixed.put(self.char, 60 , 130)  
     fixed.put(entry, 60, 100) 
     fixed.put(entry2, 60, 160) 
     btn = gtk.Button("Click me") 
     fixed.put(btn, 60, 200) 
     btn.connect("clicked", self.licked) 
     self.connect("destroy", gtk.main_quit) 
     self.add(fixed) 
     self.show_all() 
     self.e = entry 
     self.e2 = entry2 
    def licked(self, widget): 
     self.txt = self.e.get_text() 
     self.ee2 = self.e2.get_text() 
     if str(self.char.get_text()) == "*": 
      c = str(int(self.txt) * int(self.ee2)) 
      self.lbl.set_text(c) 
     elif str(self.char.get_text()) == "/": 
      c = str(int(self.txt)/int(self.ee2)) 
      self.lbl.set_text(c) 
     elif str(self.char.get_text()) == "+": 
      c = str(int(self.txt) + int(self.ee2)) 
      self.lbl.set_text(c) 
     elif str(self.char.get_text()) == "-": 
      c = str(int(self.txt) - int(self.ee2)) 
      self.lbl.set_text(c) 
     else: 
      self.lbl.set_text("Sorry plaese enter * or/or - or + !") 
PyApp() 
gtk.main() 

當我運行該程序,並輸入三個值(char和NUM1 & NUM2) 點擊後輸出單擊我按鈕是空的。

回答

0

問題是,輸出低於按鈕'「點擊我」。爲避免這種情況,請下載Label,如下所示:

fixed.put(self.lbl,60,240)