如何不断更新变量(使用带tkinter的数字调整框)?

2024-07-04 16:44:20 发布

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

我在我的程序中有两个spinbox,用tkinter创建的。我希望将用户选择的变量添加到列表中,以便稍后在程序中使用这些值。我目前遇到的问题是,这些值只在列表中存储一次,而且我无法更新它们,尽管我尝试了所有这些操作。为了清楚起见,我只希望列表中有两个值,因此当用户选择另一个数字时,它将替换存储在列表中的正确值。在

下面是我写的代码:

from tkinter import *

windowTk = Tk()
pwMain = PanedWindow(windowTk, orient=VERTICAL)
pwTop = PanedWindow(pwMain, orient=HORIZONTAL)

def configTables() :

    sLine = Spinbox(pwTop, from_=0, to=15)
    pwTop.add( Label(pwTop, text = "Combien y a-t-il de lignes de table ?") )
    pwTop.add( sLine )

    sColumn = Spinbox(pwTop, from_=0, to=15)
    pwTop.add( Label(pwTop, text = "Combien y a-t-il de colonnes de tables ?") )
    pwTop.add( sColumn )

    pwTop.pack()

    pwMain.pack()

    global coordTables
    coordTables = []
    coordTables.append( int(sLine.get()) )
    coordTables.append( int(sColumn.get()) )
    return coordTables


print( configTables() )
windowTk.mainloop()

我希望你能帮助我,所以我可以理解。在

谢谢你

LoneRetrievr公司

更新:我尝试了下面的代码,但是没有出现在窗口中(tkinter的窗口保持白色)。在

^{pr2}$

我知道它只会打印一次值,这就是我想要它做的。 你能帮助我吗?我觉得很简单,但我不知道问题出在哪里。在


Tags: 代码用户from程序add列表tkinterde
2条回答

我相信这就是你想做的。在

我建议您采用面向对象的方法。在

Tkinter有IntVar()和其他变量,非常强大。在

import tkinter as tk

class App(tk.Frame):

def __init__(self,):

    super().__init__()

    self.master.title("Hello World")
    self.lines = tk.IntVar()
    self.columns = tk.IntVar()
    self.coordTables = []
    self.init_ui()

def init_ui(self):

    self.pack(fill=tk.BOTH, expand=1,)

    f = tk.Frame()

    tk.Label(f, text = "Combien y a-t-il de lignes de tables ?").pack()
    tk.Spinbox(f, from_=0, to=15, textvariable= self.lines).pack()

    tk.Label(f, text = "Combien y a-t-il de colonnes de tables ?").pack()
    tk.Spinbox(f, from_=0, to=15, textvariable= self.columns).pack()

    w = tk.Frame()

    tk.Button(w, text="Print", command=self.on_callback).pack()
    tk.Button(w, text="Reset", command=self.on_reset).pack()
    tk.Button(w, text="Close", command=self.on_close).pack()

    f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
    w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)


def on_callback(self,):
    args = (self.lines.get(),self.columns.get())
    self.coordTables.append(args)

    print ("Numers of coords: {0}".format(len(self.coordTables)))

    for e,i in self.coordTables:
        print(e,i)

def on_reset(self):
    self.lines.set(0)
    self.columns.set(0)
    self.coordTables = []

def on_close(self):
    self.master.destroy()

如果名称='main':

app = App() app.mainloop()

可以使用IntVar将数字调整框连接到Python变量。我用一个例子扩展了你的程序。它添加了一个按钮来打印数字调整框的当前值:

from tkinter import *

windowTk = Tk()
pwMain = PanedWindow(windowTk, orient=VERTICAL)
pwTop = PanedWindow(pwMain, orient=HORIZONTAL)
lignes = IntVar(windowTk, value=0)
colonnes = IntVar(windowTk, value=0)

def print_vars():
    print(lignes.get(), colonnes.get())

def configTables() :

    sLine = Spinbox(pwTop, from_=0, to=15, textvariable=lignes)
    pwTop.add( Label(pwTop, text = "Combien y a-t-il de lignes de table ?") )
    pwTop.add( sLine )

    sColumn = Spinbox(pwTop, from_=0, to=15, textvariable=colonnes)
    pwTop.add( Label(pwTop, text = "Combien y a-t-il de colonnes de tables ?") )
    pwTop.add( sColumn )

    pwTop.pack()

    b = Button(pwMain, text='print', command=print_vars)
    b.pack(side=BOTTOM)

    pwMain.pack()

    global coordTables
    coordTables = []
    coordTables.append( int(sLine.get()) )
    coordTables.append( int(sColumn.get()) )
    return coordTables


print( configTables() )
windowTk.mainloop()

注意,我没有删除不再需要的东西,比如coordTables变量和return语句。在

相关问题 更多 >

    热门问题