|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
写了个计算器程序,文本插入采用的是insert方法,但是这样插入的文本会出现在文本框首部。比如我先insert了1,再insert2,文本框显示的是21,请问有什么解决方法?
- #!/usr/bin/python
- #-*- coding:utf-8 -*-
- import os, sys
- try:
- from tkinter import *
- except ImportError: #Python 2.x
- PythonVersion = 2
- from Tkinter import *
- from tkFont import Font
- #Usage:showinfo/warning/error,askquestion/okcancel/yesno/retrycancel
- from tkMessageBox import *
- #Usage:f=tkFileDialog.askopenfilename(initialdir='E:/Python')
- #import tkFileDialog
- #import tkSimpleDialog
- #import tkinter.filedialog as tkFileDialog
- #import tkinter.simpledialog as tkSimpleDialog #askstring()
- #所有控件和控件绑定变量引用字典,使用这个字典是为了方便在其他函数中引用所有控件。
- gComps = {}
- def Command1_Cmd(event=None):
- Text1.insert(0,"1")
- pass
- def Command2_Cmd(event=None):
- Text1.insert(0,"2")
- pass
- def Command3_Cmd(event=None):
- Text1.insert(0,"3")
- pass
- def Command4_Cmd(event=None):
- Text1.insert(0,"4")
- pass
- def Command5_Cmd(event=None):
- Text1.insert(0,"5")
- pass
- def Command6_Cmd(event=None):
- Text1.insert(0,"6")
- pass
- def Command7_Cmd(event=None):
- Text1.insert(0,"7")
- pass
- def Command8_Cmd(event=None):
- Text1.insert(0,"8")
- pass
- def Command9_Cmd(event=None):
- Text1.insert(0,"9")
- pass
- def Command_sub_Cmd(event=None):
- Text1.insert(0,"-")
- pass
- def Command_equal_Cmd(event=None):
- res=Text1.get()
- Text1.insert(0,"=%d"%eval(res))
- pass
- def Command_chu_Cmd(event=None):
- Text1.insert(0,"/")
- pass
- def Command_cheng_Cmd(event=None):
- Text1.insert(0,"*")
- pass
- def Command_add_Cmd(event=None):
- Text1.insert(0,"+")
- pass
- def main(argv):
- top = Tk()
- top.title('计算器')
- top.geometry('483x468')
- gComps['top'] = top
- Text1Var = StringVar(value='')
- global Text1
- Text1 = Entry(top, text=Text1Var)
- Text1.place(x=24, y=16, width=353, height=57)
- gComps['Text1'] = Text1
- gComps['Text1Var'] = Text1Var
- Command1 = Button(top, text='1', command=Command1_Cmd)
- Command1.place(relx=0.05, rely=0.205, relwidth=0.201, relheight=0.173)
- gComps['Command1'] = Command1
- Command2 = Button(top, text='2', command=Command2_Cmd)
- Command2.place(relx=0.315, rely=0.205, relwidth=0.201, relheight=0.173)
- gComps['Command2'] = Command2
- Command3 = Button(top, text='3', command=Command3_Cmd)
- Command3.place(relx=0.58, rely=0.205, relwidth=0.201, relheight=0.173)
- gComps['Command3'] = Command3
- Command4 = Button(top, text='4', command=Command4_Cmd)
- Command4.place(relx=0.05, rely=0.462, relwidth=0.201, relheight=0.173)
- gComps['Command4'] = Command4
- Command5 = Button(top, text='5', command=Command5_Cmd)
- Command5.place(relx=0.315, rely=0.462, relwidth=0.201, relheight=0.173)
- gComps['Command5'] = Command5
- Command6 = Button(top, text='6', command=Command6_Cmd)
- Command6.place(relx=0.58, rely=0.462, relwidth=0.201, relheight=0.173)
- gComps['Command6'] = Command6
- Command7 = Button(top, text='7', command=Command7_Cmd)
- Command7.place(relx=0.05, rely=0.718, relwidth=0.201, relheight=0.173)
- gComps['Command7'] = Command7
- Command8 = Button(top, text='8', command=Command8_Cmd)
- Command8.place(relx=0.315, rely=0.718, relwidth=0.201, relheight=0.173)
- gComps['Command8'] = Command8
- Command9 = Button(top, text='9', command=Command9_Cmd)
- Command9.place(relx=0.58, rely=0.718, relwidth=0.201, relheight=0.173)
- gComps['Command9'] = Command9
- Command_sub = Button(top, text='-', command=Command_sub_Cmd)
- Command_sub.place(relx=0.828, rely=0.222, relwidth=0.135, relheight=0.122)
- gComps['Command_sub'] = Command_sub
- Command_equal = Button(top, text='=', command=Command_equal_Cmd)
- Command_equal.place(relx=0.828, rely=0.735, relwidth=0.135, relheight=0.156)
- gComps['Command_equal'] = Command_equal
- Command_chu = Button(top, text='%', command=Command_chu_Cmd)
- Command_chu.place(relx=0.828, rely=0.564, relwidth=0.135, relheight=0.122)
- gComps['Command_chu'] = Command_chu
- Command_cheng = Button(top, text='x', command=Command_cheng_Cmd)
- Command_cheng.place(relx=0.828, rely=0.393, relwidth=0.135, relheight=0.122)
- gComps['Command_cheng'] = Command_cheng
- Command_add = Button(top, text='+', command=Command_add_Cmd)
- Command_add.place(relx=0.828, rely=0.051, relwidth=0.135, relheight=0.122)
- gComps['Command_add'] = Command_add
- top.mainloop()
- try: top.destroy()
- except: pass
- if __name__ == "__main__":
- main(sys.argv)
复制代码 |
|