if语句在pyGTK中

时间:2014-05-24 21:14:56

标签: python gtk

我正在尝试使用gtk使用python制作一个基本的计算器,但我的if语句有问题。这是代码:

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)时 单击“单击我”按钮后的输出为空。

1 个答案:

答案 0 :(得分:0)

问题是输出低于按钮'"点击我"。要避免这种情况,请按以下方式下拉Label

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