Python 当程序在tkinter中运行时,如何更新gui?

Python 当程序在tkinter中运行时,如何更新gui?,python,tkinter,Python,Tkinter,本质上,我只想在Tkinter程序运行时更新GUI上的网格,我想在程序运行时所做的更改在函数中的return true部分下,但我找不到如何做到这一点 import random from tkinter import * def input_validation(coordinates, user_input): if coordinates [0] <0 or coordinates [1] <0 or coordinates [0] >2 or coordina

本质上,我只想在Tkinter程序运行时更新GUI上的网格,我想在程序运行时所做的更改在函数中的return true部分下,但我找不到如何做到这一点

import random
from tkinter import *

def input_validation(coordinates, user_input):
    if coordinates [0] <0 or coordinates [1] <0 or coordinates [0] >2 or coordinates [1] >2:
        pass
    elif (int(user_input) == int(frame.grid_slaves(coordinates[0], coordinates[1])[0]['text'])):
        return True

    Label (frame, text = frame.grid_slaves(coordinates[0], coordinates[1])[0]['text']
           ).grid(row= previous_coordinates[0], column= previous_coordinates[1])
    Label (frame, text = "").grid(row= coordinates[0], column= coordinates[1])

def button_click():
    if (input_validation(coordinates_up, number_input.get()) == True):
        pass
    elif(input_validation(coordinates_left, number_input.get()) == True):
        pass
    elif(input_validation(coordinates_right, number_input.get()) == True):
        pass
    elif(input_validation(coordinates_down, number_input.get()) == True):
        pass
    else:
        print("hello")
        text_display.configure(text="Please input a number that is surrounding the empty space")

puzzle  = Tk()
puzzle.title("Eight Puzzle")
frame = Frame(puzzle)
space_coordinates = [2,2]

frame.pack()

number_input= Entry(frame, width= 20)
number_input.grid(row = 5, column =7)

button = Button(frame, text="Enter", command = button_click)
button.grid(row = 6, column = 7)
number = 8

text_display = Label(frame, text="Input the number you want to move into the empty space \n *make sure the number is next to the empty space*", fg="red")
text_display.grid(row = 3, column = 7)

for i in range (0,3):
    for a in range (0,3):
        if number == 0:
            Label (frame, text = " ").grid(row=i, column=a)
        else:
            Label (frame, text = number).grid(row=i, column=a)
        number= number -1

previous_coordinates = []
previous_coordinates.append(space_coordinates[0])
previous_coordinates.append(space_coordinates[1])

coordinates_up = [previous_coordinates[0], previous_coordinates[1]-1]
coordinates_left = [previous_coordinates[0]-1, previous_coordinates[1]]
coordinates_right = [previous_coordinates[0]+1, previous_coordinates[1]]
coordinates_down = [previous_coordinates[0],previous_coordinates[1]+1]

随机导入
从tkinter进口*
def输入验证(坐标、用户输入):
如果坐标[0]2:
通过
elif(int(用户输入)==int(frame.grid(坐标[0],坐标[1])[0]['text'])):
返回真值
标签(frame,text=frame.grid_slaves(坐标[0],坐标[1])[0]['text']
).grid(行=上一个坐标[0],列=上一个坐标[1])
标签(frame,text=”“).grid(行=坐标[0],列=坐标[1])
def按钮单击()
如果(输入\验证(坐标向上,数字\输入.get())==True):
通过
elif(输入\验证(左坐标,数字\输入.get())==True):
通过
elif(输入\验证(坐标\右,数字\输入.get())==True):
通过
elif(输入验证(坐标向下,编号为input.get())==True):
通过
其他:
打印(“你好”)
text_display.configure(text=“请输入一个围绕空白的数字”)
拼图=Tk()
拼图。标题(“八个拼图”)
框架=框架(拼图)
空间坐标=[2,2]
frame.pack()
数字\输入=输入(帧,宽度=20)
数字输入网格(行=5,列=7)
按钮=按钮(框,text=“回车”,命令=按钮
按钮网格(行=6,列=7)
数字=8
text\u display=Label(frame,text=“输入要移动到空白处的号码\n*确保号码在空白处的旁边*”,fg=“红色”)
文本显示网格(行=3,列=7)
对于范围(0,3)内的i:
对于范围(0,3)内的a:
如果数字==0:
标签(frame,text=”“).grid(行=i,列=a)
其他:
标签(框架,文本=编号)。网格(行=i,列=a)
数字=数字-1
以前的_坐标=[]
上一个坐标。追加(空间坐标[0])
上一个_坐标。追加(空格_坐标[1])
坐标上=[上一个坐标[0],上一个坐标[1]-1]
左坐标=[先前的坐标[0]-1,先前的坐标[1]]
右坐标=[上一个坐标[0]+1,上一个坐标[1]]
坐标向下=[上一个坐标[0],上一个坐标[1]+1]

您需要在
tkinter
程序结束时使用
puzzle.mainloop()
。另外,如果要更新window mid函数,可以使用:
window.update()

您正在查找
puzzle.mainloop()
?试着把它放在你的结尾code@TheLizzard这有助于在指定的部分更新GUI吗?它会告诉tkinter它需要进入它的main循环,这基本上是一个
,而True
循环会更新GUI直到窗口关闭。我可以使用window.update()吗按原样?将
窗口
替换为您的窗口名称(在您的示例中为
puzzle.update()
)是的,它仍然不起作用