Entry设置文本框内容
为什么Entry的初始文本不是aaa呢?如下:class Application_ui(Frame):#这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('新增')
self.master.geometry('865x525')
self.master.resizable(0,0)
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
if opn == 2: # True
self.inportVar = StringVar(value='aaa')
else:
self.inportVar = StringVar(value='')
self.inport = Entry(self.top, textvariable=self.inportVar, font=('宋体',9))
self.inport.place(relx=0.675, rely=0.061, relwidth=0.112, relheight=0.048)
试试是不是opn == 2为false先 灰晨 发表于 2021-7-13 15:50
试试是不是opn == 2为false先
这个是True 你这类里没看见那有定义opn啊,应该是别的地方定义的吧,你只发这段我也看不出啦{:10_258:}
StringVar()我不知道能不能像你那么用
不过我都是
self.inportVar = StringVar()
self.inportVar.set('aaa')
自己试试先 我简单测试了一下这段代码,只要前面加个opn=2就能正常运行,应该是条件不符合的原因
from tkinter import *
class Application_ui(Frame):
#这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('新增')
self.master.geometry('865x525')
self.master.resizable(0,0)
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
#self.style = Style()
opn = 2
if opn == 2: # True
self.inportVar = StringVar(value='aaa')
else:
self.inportVar = StringVar(value='')
self.inport = Entry(self.top, textvariable=self.inportVar, font=('宋体',9))
self.inport.place(relx=0.675, rely=0.061, relwidth=0.112, relheight=0.048)
if __name__ == '__main__':
Application_ui()
本帖最后由 Wirror 于 2021-7-13 17:10 编辑
逃兵 发表于 2021-7-13 16:45
我简单测试了一下这段代码,只要前面加个opn=2就能正常运行,应该是条件不符合的原因
可以了 灰晨 发表于 2021-7-13 16:19
你这类里没看见那有定义opn啊,应该是别的地方定义的吧,你只发这段我也看不出啦
是在别的地方定义的 本帖最后由 Wirror 于 2021-7-13 17:44 编辑
逃兵 发表于 2021-7-13 16:45
我简单测试了一下这段代码,只要前面加个opn=2就能正常运行,应该是条件不符合的原因
可是为什么我这里还是不行啊
完整代码:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import pymysql
import tkinter.messagebox
import os, sys
try:
from tkinter import *
except ImportError:#Python 2.x
PythonVersion = 2
from Tkinter import *
from tkFont import Font
from ttk import *
#Usage:showinfo/warning/error,askquestion/okcancel/yesno/retrycancel
from tkMessageBox import *
#Usage:f=tkFileDialog.askopenfilename(initialdir='E:/Python')
#import tkFileDialog
#import tkSimpleDialog
else:#Python 3.x
PythonVersion = 3
from tkinter.font import Font
from tkinter.ttk import *
from tkinter.messagebox import *
#import tkinter.filedialog as tkFileDialog
#import tkinter.simpledialog as tkSimpleDialog #askstring()
class Application_ui(Frame):
#这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('新增商品')
if opn == 2:
self.master.title('修改商品')
self.master.geometry('865x525')
self.master.resizable(0,0)
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
self.style.configure('Label1.TLabel',anchor='w', font=('宋体',9))
self.Label1 = Label(self.top, text='名称', style='Label1.TLabel')
self.Label1.place(relx=0.046, rely=0.061, relwidth=0.112, relheight=0.093)
self.style.configure('Label2.TLabel',anchor='w', font=('宋体',9))
self.Label2 = Label(self.top, text='', style='Label2.TLabel')
self.Label2.place(relx=0.046, rely=0.244, relwidth=0.112, relheight=0.093)
self.style.configure('Label3.TLabel',anchor='w', font=('宋体',9))
self.Label3 = Label(self.top, text='', style='Label3.TLabel')
self.Label3.place(relx=0.046, rely=0.427, relwidth=0.112, relheight=0.093)
self.style.configure('Label4.TLabel',anchor='w', font=('宋体',9))
self.Label4 = Label(self.top, text='', style='Label4.TLabel')
self.Label4.place(relx=0.037, rely=0.61, relwidth=0.112, relheight=0.093)
self.style.configure('Label5.TLabel',anchor='w', font=('宋体',9))
self.Label5 = Label(self.top, text='', style='Label5.TLabel')
self.Label5.place(relx=0.518, rely=0.061, relwidth=0.112, relheight=0.093)
self.style.configure('Label6.TLabel',anchor='w', font=('宋体',9))
self.Label6 = Label(self.top, text='', style='Label6.TLabel')
self.Label6.place(relx=0.527, rely=0.29, relwidth=0.112, relheight=0.093)
self.style.configure('Label7.TLabel',anchor='w', font=('宋体',9))
self.Label7 = Label(self.top, text='', style='Label7.TLabel')
self.Label7.place(relx=0.536, rely=0.503, relwidth=0.112, relheight=0.093)
self.nameVar = StringVar(value='')
self.name = Entry(self.top, textvariable=self.nameVar, font=('宋体',9))
self.name.place(relx=0.148, rely=0.061, relwidth=0.251, relheight=0.048)
self.codeVar = StringVar(value='')
self.code = Entry(self.top, textvariable=self.codeVar, font=('宋体',9))
self.code.place(relx=0.148, rely=0.239, relwidth=0.251, relheight=0.048)
self.brandVar = StringVar(value='')
self.brand = Entry(self.top, textvariable=self.brandVar, font=('宋体',9))
self.brand.place(relx=0.148, rely=0.417, relwidth=0.251, relheight=0.048)
self.factoryVar = StringVar(value='')
self.factory = Entry(self.top, textvariable=self.factoryVar, font=('宋体',9))
self.factory.place(relx=0.148, rely=0.594, relwidth=0.251, relheight=0.048)
opn = 2
if opn == 2:
self.inportVar = tkinter.StringVar()
self.inportVar.set('aaa')
else:
self.inportVar = tkinter.StringVar(value='')
self.inport = Entry(self.top, textvariable=self.inportVar, font=('宋体', 9))
self.inport.place(relx=0.675, rely=0.061, relwidth=0.112, relheight=0.048)
self.sellVar = StringVar(value='')
self.sell = Entry(self.top, textvariable=self.sellVar, font=('宋体',9))
self.sell.place(relx=0.675, rely=0.274, relwidth=0.112, relheight=0.048)
self.discountVar = StringVar(value='')
self.discount = Entry(self.top, textvariable=self.discountVar, font=('宋体',9))
self.discount.place(relx=0.675, rely=0.488, relwidth=0.112, relheight=0.048)
self.style.configure('Command1.TButton',font=('宋体',9))
self.Command1 = Button(self.top, text='提交', command=self.Command1_Cmd, style='Command1.TButton')
self.Command1.place(relx=0.157, rely=0.731, relwidth=0.177, relheight=0.124)
self.style.configure('Command2.TButton',font=('宋体',9))
self.Command2 = Button(self.top, text='重置', command=self.Command2_Cmd, style='Command2.TButton')
self.Command2.place(relx=0.555, rely=0.731, relwidth=0.177, relheight=0.124)
class Application(Application_ui):
#这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
def __init__(self, master=None):
Application_ui.__init__(self, master)
def Command1_Cmd(self, event=None):
res = tkinter.messagebox.askokcancel \
(title='提示', message='确定添加?')
if res == True:
conn = pymysql.connect(host='localhost',
user="root",
passwd="1234")
cursor = conn.cursor()# 建立游标
cursor.execute('use good_db')
cursor.execute('set names latin1')
cursor.execute('select id from {}'.format(thename))
values = cursor.fetchall()
r = 0
print(values)
for value in values:
if value >= r:
r = value + 1
cursor.execute('set names latin1')
try:
cursor.execute('select code from {} where code = {}'.format(thename, self.code.get()))
judge = cursor.fetchone()
if judge == None:
cursor.execute('insert into {} values({},\'{}\',\'{}\',\'{}\',\'{}\',{},\'{}\',{},{},{});'.format(thename,r,
self.code.get(), self.name.get(), self.brand.get(), thename, 0 , self.factory.get(), self.sell.get(),
self.inport.get(), self.discount.get()))
conn.commit()
cursor.close()# 先关闭游标
conn.close()# 再关闭数据库连接
res = tkinter.messagebox.askokcancel \
(title='提示', message='添加成功!')
else:
tkinter.messagebox.askokcancel \
(title='提示', message='不能添加!')
self.Command2_Cmd()
except:
tkinter.messagebox.askokcancel \
(title='提示', message='请输入数字!')
def Command2_Cmd(self, event=None):
self.inport.delete(0, END)
self.code.delete(0, END)
self.name.delete(0, END)
self.brand.delete(0, END)
self.sell.delete(0, END)
self.discount.delete(0, END)
self.factory.delete(0, END)
def add_good(tablename, o, tar):
global thename
global opn
global target
target = tar
opn = o
thename = tablename
add_good_window = Tk()
Application(add_good_window).mainloop()
页:
[1]