get()项不返回任何tkin

2024-09-30 14:22:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用类abd的tkinter我在添加产品时遇到了问题

class Add_Page():
  def __init__(self, child):
    self.child = child
    child.title = "Ajouter"

    self.l1=Label(child,text="Ajouter produit :",bg="blue").grid(row=0,columnspan=2)

    self.l2=Label(child,text="Matricule").grid(row=1,column=0) 
    self.vlrm = StringVar() 
    self.en2 = Entry(child, textvariable=self.vlrm, width=30).grid(row=1,column=1)

    self.l3=Label(child,text="Nom").grid(row=2,column=0)
    self.vlrn = StringVar() 
    self.en3 = Entry(child, textvariable=self.vlrn, width=30).grid(row=2,column=1)

    self.l4=Label(child,text="Prix").grid(row=3,column=0)
    self.vlrp = IntVar() 
    self.en4 = Entry(child, textvariable=self.vlrp, width=30).grid(row=3,column=1)

    self.b2=Button(child,text="Valider",command=self.add_p).grid(row=4,columnspan=2)

  #Add product function
  def add_p(self):

    print(self.vlrm.get())
    print(self.vlrp.get())

结果是一个空链和0 我似乎没有发现这个问题,特别是我在users类中使用了get方法,它工作得很好 希普


Tags: textselfaddchildgetdefcolumnwidth
1条回答
网友
1楼 · 发布于 2024-09-30 14:22:05

你不需要创建一个变量来输入,只需要为radiobutton或checkbutton创建一个变量。你可以改变tkinter对象的创建,就像这样

改变这个

self.l1=Label(child,text="Ajouter produit :",bg="blue").grid(row=0,columnspan=2)

为了这个

self.l1 = Label(child, text = "Ajouter produit :", bg = "blue")
self.l1.grid(row = 0, columnspan = 2) # remove all variables StringVar() and IntVar()

如果将来需要使用.config或.get()进行一些更改,则可以在第一个示例中进行更改。您可以继续使用variable,但我不建议这样做,如果您进行此更改,get()现在就可以工作了

我在tkinter中做了一个简单的画法,可以使用也可以修改,是针对Python2的

from Tkinter import *

class Draw_tk():
    Row, Column, List = 0, 0, []
    def __init__(self, child):
        self.child = child
        child.title = "Ajouter"
    def labelAndEntry(self, text): # def to create a entry and a label
        self.l = Label(self.child, text = text) # create label
        self.l.grid(row = Draw_tk.Row, column = Draw_tk.Column) # place label
        Draw_tk.Column += 1 # add 1 in Column to place the entry
        self.e = Entry(self.child, width = 30) # create entry
        self.e.grid(row = Draw_tk.Row, column = Draw_tk.Column) # place entry
        Draw_tk.List.append(self.e) # add the entry in a list
        Draw_tk.Row, Draw_tk.Column = Draw_tk.Row + 1, 0
    def label(self, text):
        self.l = Label(self.child, text = text, bg = "blue") # def to create a simple label
        self.l.grid(row = Draw_tk.Row, columnspan=2) # place the label
        Draw_tk.Row += 1
    def button(self, text, var): # create a simple button
        self.b = Button(self.child, text = text, command = var) # create button
        self.b.grid(row = Draw_tk.Row, column = Draw_tk.Column) # place the button

def valid():
    for item in Draw_tk.List: # run a variable in your values list
        print item.get() # get the value and print

root = Tk()
controller = Draw_tk(root) # create object Draw_tk
controller.label('Ajouter produit')
controller.labelAndEntry('Matricule')
controller.labelAndEntry('Nom')
controller.labelAndEntry('Prix')
controller.button('Valider', valid)
root.mainloop()

相关问题 更多 >