Форум сайта python.su
Вот мои наброски кода:
from tkinter import * def ha_one(event): tex1.insert(END, "1") def ha_two(event): tex1.insert(END, "2") def sum1(event): tex1.insert(END, "+") def eq1(event): c = a + b tex1.delete() tex1.insert(c) root = Tk() if sum1 == True: a = int(tex1.get()) if eq1 == True: b = int(tex1.get()) fra0 = Frame(root, width=400, height=50, bg="#ffe240") fra1 = Frame(root, width=400, height=150, bg="#ffeed0") tex1 = Text(fra0, width=30, height=3, font="Verdana 12", wrap=WORD) but1 = Button(fra1, text="1") but2 = Button(fra1, text="2") butSum = Button(fra1, text="+") butEqual = Button(fra1, text="=") but1.bind("<Button-1>", ha_one) but2.bind("<Button-1>", ha_two) butSum.bind("<Button-1>", sum1) butEqual.bind("<Button-1>", eq1) fra0.pack() tex1.pack() fra1.pack() but1.pack() but2.pack() butSum.pack() butEqual.pack() root.mainloop()
Отредактировано zipsetic (Апрель 17, 2012 17:41:18)
Офлайн
… Но он не рабочий. Скажите, правильно ли я начал писать код?
Но он не рабочи
правильно ли я начал писать кодНет, Ваш КО
Офлайн
Да мне бы только показать, как правильно начать писать эту программу, я новичек, и это первая моя программа с GUI на tkinter…
Офлайн
zipseticВо-первых оформи код в тегах
Да мне бы только показать, как правильно начать писать эту программу, я новичек, и это первая моя программа с GUI на tkinter…
[code python][/code]
tex1.insert©
root = Tk()
Отредактировано FishHook (Апрель 12, 2012 05:46:50)
Прикреплённый файлы:
Screenshot-2.png (3,9 KБ)
Офлайн
# -*- coding: utf-8 -*- from Tkinter import * class CalcButton(Button): def __init__(self,*args,**kw): Button.__init__(self,*args,**kw) self.__val='' @property def Value(self): return self.__val @Value.setter def Value(self,val): self.__val=str(val) def set_command(self,obj): self.configure(command=lambda : obj.text.insert(END,self.Value)) class Calc(object): def __init__(self, master): self.master=master self.COMMANDS=['+','-','*','/','.'] self.text_frame=Frame(master=self.master) self.text_frame.pack() self.text=Text(master=self.text_frame, width=30, height=3, font="Verdana 12") self.text.pack() self.int_common_frame=Frame(master=self.master) self.int_common_frame.pack(side=LEFT, padx=10) self.int_frames=[ Frame(master=self.int_common_frame) for _ in range(4) ] for fr in self.int_frames: fr.pack() self.commands_frame=Frame(master=self.master) self.commands_frame.pack(side=LEFT, padx=10, pady=10) self.int_buttons=[CalcButton( master=self.int_frames[i/3], text=i) for i in range(10)] self.command_buttons=[CalcButton(master=self.commands_frame, text=c) for c in self.COMMANDS] self.pac_buttons(self.int_buttons, LEFT) self.pac_buttons(self.command_buttons,BOTTOM) self.enter_frame=Frame(master=self.master) self.enter_frame.pack(side=LEFT) self.enter_button=Button(master=self.enter_frame, text='=', command=self.calculate) self.enter_button.pack() self.clear_button=Button(master=self.enter_frame, text=' C ', command=self.clear) self.clear_button.pack() def pac_buttons(self,l,side): for bt in l: bt.Value=bt.config()['text'][4] bt.set_command(self) bt.pack(side=side) def calculate(self): try: result=eval(self.text.get(1.0,END)) except SyntaxError:pass except ZeroDivisionError: self.text.delete(1.0,END) self.text.insert(END,u'На ноль делишь, сабака!') else: self.text.delete(1.0,END) self.text.insert(END,result) def clear(self): self.text.delete(1.0,END) if __name__=="__main__": root = Tk() app = Calc(root) root.mainloop()
Отредактировано FishHook (Апрель 12, 2012 09:02:14)
Офлайн