鱼C论坛

 找回密码
 立即注册
查看: 648|回复: 1

关于tkinter的entry控件文本插入

[复制链接]
发表于 2019-6-15 19:25:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写了个计算器程序,文本插入采用的是insert方法,但是这样插入的文本会出现在文本框首部。比如我先insert了1,再insert2,文本框显示的是21,请问有什么解决方法?
  1. #!/usr/bin/python
  2. #-*- coding:utf-8 -*-

  3. import os, sys
  4. try:
  5.     from tkinter import *
  6. except ImportError:  #Python 2.x
  7.     PythonVersion = 2
  8.     from Tkinter import *
  9.     from tkFont import Font
  10.     #Usage:showinfo/warning/error,askquestion/okcancel/yesno/retrycancel
  11.     from tkMessageBox import *
  12.     #Usage:f=tkFileDialog.askopenfilename(initialdir='E:/Python')
  13.     #import tkFileDialog
  14.     #import tkSimpleDialog
  15.     #import tkinter.filedialog as tkFileDialog
  16.     #import tkinter.simpledialog as tkSimpleDialog    #askstring()


  17. #所有控件和控件绑定变量引用字典,使用这个字典是为了方便在其他函数中引用所有控件。
  18. gComps = {}


  19. def Command1_Cmd(event=None):
  20.     Text1.insert(0,"1")
  21.     pass

  22. def Command2_Cmd(event=None):
  23.     Text1.insert(0,"2")
  24.     pass

  25. def Command3_Cmd(event=None):
  26.    Text1.insert(0,"3")
  27.    pass

  28. def Command4_Cmd(event=None):
  29.     Text1.insert(0,"4")
  30.     pass

  31. def Command5_Cmd(event=None):
  32.     Text1.insert(0,"5")
  33.     pass

  34. def Command6_Cmd(event=None):
  35.     Text1.insert(0,"6")
  36.     pass

  37. def Command7_Cmd(event=None):
  38.     Text1.insert(0,"7")
  39.     pass

  40. def Command8_Cmd(event=None):
  41.     Text1.insert(0,"8")
  42.     pass

  43. def Command9_Cmd(event=None):
  44.     Text1.insert(0,"9")
  45.     pass

  46. def Command_sub_Cmd(event=None):
  47.     Text1.insert(0,"-")
  48.     pass

  49. def Command_equal_Cmd(event=None):
  50.     res=Text1.get()
  51.     Text1.insert(0,"=%d"%eval(res))
  52.     pass

  53. def Command_chu_Cmd(event=None):
  54.     Text1.insert(0,"/")
  55.     pass

  56. def Command_cheng_Cmd(event=None):
  57.     Text1.insert(0,"*")
  58.     pass

  59. def Command_add_Cmd(event=None):
  60.     Text1.insert(0,"+")
  61.     pass


  62. def main(argv):
  63.     top = Tk()
  64.     top.title('计算器')
  65.     top.geometry('483x468')
  66.     gComps['top'] = top

  67.     Text1Var = StringVar(value='')
  68.     global Text1
  69.     Text1 = Entry(top, text=Text1Var)
  70.     Text1.place(x=24, y=16, width=353, height=57)
  71.     gComps['Text1'] = Text1
  72.     gComps['Text1Var'] = Text1Var

  73.     Command1 = Button(top, text='1', command=Command1_Cmd)
  74.     Command1.place(relx=0.05, rely=0.205, relwidth=0.201, relheight=0.173)
  75.     gComps['Command1'] = Command1

  76.     Command2 = Button(top, text='2', command=Command2_Cmd)
  77.     Command2.place(relx=0.315, rely=0.205, relwidth=0.201, relheight=0.173)
  78.     gComps['Command2'] = Command2

  79.     Command3 = Button(top, text='3', command=Command3_Cmd)
  80.     Command3.place(relx=0.58, rely=0.205, relwidth=0.201, relheight=0.173)
  81.     gComps['Command3'] = Command3

  82.     Command4 = Button(top, text='4', command=Command4_Cmd)
  83.     Command4.place(relx=0.05, rely=0.462, relwidth=0.201, relheight=0.173)
  84.     gComps['Command4'] = Command4

  85.     Command5 = Button(top, text='5', command=Command5_Cmd)
  86.     Command5.place(relx=0.315, rely=0.462, relwidth=0.201, relheight=0.173)
  87.     gComps['Command5'] = Command5

  88.     Command6 = Button(top, text='6', command=Command6_Cmd)
  89.     Command6.place(relx=0.58, rely=0.462, relwidth=0.201, relheight=0.173)
  90.     gComps['Command6'] = Command6

  91.     Command7 = Button(top, text='7', command=Command7_Cmd)
  92.     Command7.place(relx=0.05, rely=0.718, relwidth=0.201, relheight=0.173)
  93.     gComps['Command7'] = Command7

  94.     Command8 = Button(top, text='8', command=Command8_Cmd)
  95.     Command8.place(relx=0.315, rely=0.718, relwidth=0.201, relheight=0.173)
  96.     gComps['Command8'] = Command8

  97.     Command9 = Button(top, text='9', command=Command9_Cmd)
  98.     Command9.place(relx=0.58, rely=0.718, relwidth=0.201, relheight=0.173)
  99.     gComps['Command9'] = Command9

  100.     Command_sub = Button(top, text='-', command=Command_sub_Cmd)
  101.     Command_sub.place(relx=0.828, rely=0.222, relwidth=0.135, relheight=0.122)
  102.     gComps['Command_sub'] = Command_sub

  103.     Command_equal = Button(top, text='=', command=Command_equal_Cmd)
  104.     Command_equal.place(relx=0.828, rely=0.735, relwidth=0.135, relheight=0.156)
  105.     gComps['Command_equal'] = Command_equal

  106.     Command_chu = Button(top, text='%', command=Command_chu_Cmd)
  107.     Command_chu.place(relx=0.828, rely=0.564, relwidth=0.135, relheight=0.122)
  108.     gComps['Command_chu'] = Command_chu

  109.     Command_cheng = Button(top, text='x', command=Command_cheng_Cmd)
  110.     Command_cheng.place(relx=0.828, rely=0.393, relwidth=0.135, relheight=0.122)
  111.     gComps['Command_cheng'] = Command_cheng

  112.     Command_add = Button(top, text='+', command=Command_add_Cmd)
  113.     Command_add.place(relx=0.828, rely=0.051, relwidth=0.135, relheight=0.122)
  114.     gComps['Command_add'] = Command_add

  115.     top.mainloop()
  116.     try: top.destroy()
  117.     except: pass



  118. if __name__ == "__main__":
  119.     main(sys.argv)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-6-15 19:26:10 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-16 10:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表