鱼C论坛

 找回密码
 立即注册
查看: 2338|回复: 8

Entry设置文本框内容

[复制链接]
发表于 2021-7-13 15:32:47 | 显示全部楼层 |阅读模式

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

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

x
为什么Entry的初始文本不是aaa呢?如下:
  1. class Application_ui(Frame):
  2.     #这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
  3.     def __init__(self, master=None):
  4.         Frame.__init__(self, master)

  5.         self.master.title('新增')
  6.         self.master.geometry('865x525')
  7.         self.master.resizable(0,0)
  8.         self.createWidgets()

  9.     def createWidgets(self):
  10.         self.top = self.winfo_toplevel()

  11.         self.style = Style()



  12.         if opn == 2:                 # True
  13.             self.inportVar = StringVar(value='aaa')
  14.         else:
  15.             self.inportVar = StringVar(value='')
  16.         self.inport = Entry(self.top, textvariable=self.inportVar, font=('宋体',9))
  17.         self.inport.place(relx=0.675, rely=0.061, relwidth=0.112, relheight=0.048)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-13 15:50:09 | 显示全部楼层
试试是不是opn == 2为false先
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-13 16:13:36 | 显示全部楼层
灰晨 发表于 2021-7-13 15:50
试试是不是opn == 2为false先

这个是True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-13 16:19:24 | 显示全部楼层
你这类里没看见那有定义opn啊,应该是别的地方定义的吧,你只发这段我也看不出啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-13 16:28:22 | 显示全部楼层
StringVar()我不知道能不能像你那么用
不过我都是
self.inportVar = StringVar()
self.inportVar.set('aaa')
自己试试先
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-13 16:45:54 | 显示全部楼层
我简单测试了一下这段代码,只要前面加个opn=2就能正常运行,应该是条件不符合的原因

  1. from tkinter import *
  2. class Application_ui(Frame):
  3.     #这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
  4.     def __init__(self, master=None):
  5.         Frame.__init__(self, master)

  6.         self.master.title('新增')
  7.         self.master.geometry('865x525')
  8.         self.master.resizable(0,0)
  9.         self.createWidgets()

  10.     def createWidgets(self):
  11.         self.top = self.winfo_toplevel()

  12.         #self.style = Style()
  13.         
  14.         opn = 2
  15.         
  16.         if opn == 2:                 # True
  17.             self.inportVar = StringVar(value='aaa')
  18.         else:
  19.             self.inportVar = StringVar(value='')
  20.         self.inport = Entry(self.top, textvariable=self.inportVar, font=('宋体',9))
  21.         self.inport.place(relx=0.675, rely=0.061, relwidth=0.112, relheight=0.048)

  22. if __name__ == '__main__':
  23.     Application_ui()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-13 17:05:14 | 显示全部楼层
本帖最后由 Wirror 于 2021-7-13 17:10 编辑
逃兵 发表于 2021-7-13 16:45
我简单测试了一下这段代码,只要前面加个opn=2就能正常运行,应该是条件不符合的原因

可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-13 17:05:44 | 显示全部楼层
灰晨 发表于 2021-7-13 16:19
你这类里没看见那有定义opn啊,应该是别的地方定义的吧,你只发这段我也看不出啦

是在别的地方定义的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-13 17:42:18 | 显示全部楼层
本帖最后由 Wirror 于 2021-7-13 17:44 编辑
逃兵 发表于 2021-7-13 16:45
我简单测试了一下这段代码,只要前面加个opn=2就能正常运行,应该是条件不符合的原因


可是为什么我这里还是不行啊
完整代码:
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. import pymysql
  4. import tkinter.messagebox
  5. import os, sys
  6. try:
  7.     from tkinter import *
  8. except ImportError:  #Python 2.x
  9.     PythonVersion = 2
  10.     from Tkinter import *
  11.     from tkFont import Font
  12.     from ttk import *
  13.     #Usage:showinfo/warning/error,askquestion/okcancel/yesno/retrycancel
  14.     from tkMessageBox import *
  15.     #Usage:f=tkFileDialog.askopenfilename(initialdir='E:/Python')
  16.     #import tkFileDialog
  17.     #import tkSimpleDialog
  18. else:  #Python 3.x
  19.     PythonVersion = 3
  20.     from tkinter.font import Font
  21.     from tkinter.ttk import *
  22.     from tkinter.messagebox import *
  23.     #import tkinter.filedialog as tkFileDialog
  24.     #import tkinter.simpledialog as tkSimpleDialog    #askstring()

  25. class Application_ui(Frame):
  26.     #这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
  27.     def __init__(self, master=None):
  28.         Frame.__init__(self, master)

  29.         self.master.title('新增商品')
  30.         if opn == 2:
  31.             self.master.title('修改商品')
  32.         self.master.geometry('865x525')
  33.         self.master.resizable(0,0)
  34.         self.createWidgets()

  35.     def createWidgets(self):
  36.         self.top = self.winfo_toplevel()

  37.         self.style = Style()

  38.         self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
  39.         self.Label1 = Label(self.top, text='名称', style='Label1.TLabel')
  40.         self.Label1.place(relx=0.046, rely=0.061, relwidth=0.112, relheight=0.093)

  41.         self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
  42.         self.Label2 = Label(self.top, text='', style='Label2.TLabel')
  43.         self.Label2.place(relx=0.046, rely=0.244, relwidth=0.112, relheight=0.093)

  44.         self.style.configure('Label3.TLabel',anchor='w', font=('宋体',9))
  45.         self.Label3 = Label(self.top, text='', style='Label3.TLabel')
  46.         self.Label3.place(relx=0.046, rely=0.427, relwidth=0.112, relheight=0.093)

  47.         self.style.configure('Label4.TLabel',anchor='w', font=('宋体',9))
  48.         self.Label4 = Label(self.top, text='', style='Label4.TLabel')
  49.         self.Label4.place(relx=0.037, rely=0.61, relwidth=0.112, relheight=0.093)

  50.         self.style.configure('Label5.TLabel',anchor='w', font=('宋体',9))
  51.         self.Label5 = Label(self.top, text='', style='Label5.TLabel')
  52.         self.Label5.place(relx=0.518, rely=0.061, relwidth=0.112, relheight=0.093)

  53.         self.style.configure('Label6.TLabel',anchor='w', font=('宋体',9))
  54.         self.Label6 = Label(self.top, text='', style='Label6.TLabel')
  55.         self.Label6.place(relx=0.527, rely=0.29, relwidth=0.112, relheight=0.093)

  56.         self.style.configure('Label7.TLabel',anchor='w', font=('宋体',9))
  57.         self.Label7 = Label(self.top, text='', style='Label7.TLabel')
  58.         self.Label7.place(relx=0.536, rely=0.503, relwidth=0.112, relheight=0.093)

  59.         self.nameVar = StringVar(value='')
  60.         self.name = Entry(self.top, textvariable=self.nameVar, font=('宋体',9))
  61.         self.name.place(relx=0.148, rely=0.061, relwidth=0.251, relheight=0.048)

  62.         self.codeVar = StringVar(value='')
  63.         self.code = Entry(self.top, textvariable=self.codeVar, font=('宋体',9))
  64.         self.code.place(relx=0.148, rely=0.239, relwidth=0.251, relheight=0.048)

  65.         self.brandVar = StringVar(value='')
  66.         self.brand = Entry(self.top, textvariable=self.brandVar, font=('宋体',9))
  67.         self.brand.place(relx=0.148, rely=0.417, relwidth=0.251, relheight=0.048)

  68.         self.factoryVar = StringVar(value='')
  69.         self.factory = Entry(self.top, textvariable=self.factoryVar, font=('宋体',9))
  70.         self.factory.place(relx=0.148, rely=0.594, relwidth=0.251, relheight=0.048)

  71.         opn = 2
  72.         if opn == 2:
  73.             self.inportVar = tkinter.StringVar()
  74.             self.inportVar.set('aaa')
  75.         else:
  76.             self.inportVar = tkinter.StringVar(value='')
  77.         self.inport = Entry(self.top, textvariable=self.inportVar, font=('宋体', 9))
  78.         self.inport.place(relx=0.675, rely=0.061, relwidth=0.112, relheight=0.048)

  79.         self.sellVar = StringVar(value='')
  80.         self.sell = Entry(self.top, textvariable=self.sellVar, font=('宋体',9))
  81.         self.sell.place(relx=0.675, rely=0.274, relwidth=0.112, relheight=0.048)

  82.         self.discountVar = StringVar(value='')
  83.         self.discount = Entry(self.top, textvariable=self.discountVar, font=('宋体',9))
  84.         self.discount.place(relx=0.675, rely=0.488, relwidth=0.112, relheight=0.048)

  85.         self.style.configure('Command1.TButton',font=('宋体',9))
  86.         self.Command1 = Button(self.top, text='提交', command=self.Command1_Cmd, style='Command1.TButton')
  87.         self.Command1.place(relx=0.157, rely=0.731, relwidth=0.177, relheight=0.124)

  88.         self.style.configure('Command2.TButton',font=('宋体',9))
  89.         self.Command2 = Button(self.top, text='重置', command=self.Command2_Cmd, style='Command2.TButton')
  90.         self.Command2.place(relx=0.555, rely=0.731, relwidth=0.177, relheight=0.124)


  91. class Application(Application_ui):
  92.     #这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
  93.     def __init__(self, master=None):
  94.         Application_ui.__init__(self, master)

  95.     def Command1_Cmd(self, event=None):
  96.         res = tkinter.messagebox.askokcancel \
  97.             (title='提示', message='确定添加?')
  98.         if res == True:
  99.             conn = pymysql.connect(host='localhost',
  100.                                    user="root",
  101.                                    passwd="1234")
  102.             cursor = conn.cursor()  # 建立游标
  103.             cursor.execute('use good_db')
  104.             cursor.execute('set names latin1')
  105.             cursor.execute('select id from {}'.format(thename))
  106.             values = cursor.fetchall()
  107.             r = 0
  108.             print(values)
  109.             for value in values:
  110.                 if value[0] >= r:
  111.                     r = value[0] + 1
  112.             cursor.execute('set names latin1')
  113.             try:
  114.                 cursor.execute('select code from {} where code = {}'.format(thename, self.code.get()))
  115.                 judge = cursor.fetchone()
  116.                 if judge == None:
  117.                     cursor.execute('insert into {} values({},\'{}\',\'{}\',\'{}\',\'{}\',{},\'{}\',{},{},{});'.format(thename,r,
  118.                     self.code.get(), self.name.get(), self.brand.get(), thename, 0 , self.factory.get(), self.sell.get(),
  119.                                    self.inport.get(), self.discount.get()))

  120.                     conn.commit()
  121.                     cursor.close()  # 先关闭游标
  122.                     conn.close()  # 再关闭数据库连接
  123.                     res = tkinter.messagebox.askokcancel \
  124.                         (title='提示', message='添加成功!')
  125.                 else:
  126.                     tkinter.messagebox.askokcancel \
  127.                 (title='提示', message='不能添加!')
  128.                     self.Command2_Cmd()
  129.             except:
  130.                 tkinter.messagebox.askokcancel \
  131.                             (title='提示', message='请输入数字!')



  132.     def Command2_Cmd(self, event=None):
  133.         self.inport.delete(0, END)
  134.         self.code.delete(0, END)
  135.         self.name.delete(0, END)
  136.         self.brand.delete(0, END)
  137.         self.sell.delete(0, END)
  138.         self.discount.delete(0, END)
  139.         self.factory.delete(0, END)

  140. def add_good(tablename, o, tar):
  141.     global thename
  142.     global opn
  143.     global target
  144.     target = tar
  145.     opn = o
  146.     thename = tablename
  147.     add_good_window = Tk()
  148.     Application(add_good_window).mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 18:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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