单击后是否更新按钮的位置?(Tkinter Python GUI)
启动游戏会将按钮按随机顺序排列(由函数引起),但当我继续按“正确”按钮时,按钮的位置在加载屏幕后不会更新;即使他们的文字更新。即使在randomPosition功能更改每个按钮的位置后,按钮的位置也不会更新有没有办法更新按钮的位置,使“正确”的按钮在加载屏幕后不在同一位置?单击后是否更新按钮的位置?(Tkinter Python GUI),python,python-3.x,button,tkinter,Python,Python 3.x,Button,Tkinter,启动游戏会将按钮按随机顺序排列(由函数引起),但当我继续按“正确”按钮时,按钮的位置在加载屏幕后不会更新;即使他们的文字更新。即使在randomPosition功能更改每个按钮的位置后,按钮的位置也不会更新有没有办法更新按钮的位置,使“正确”的按钮在加载屏幕后不在同一位置? import tkinter as tk from tkinter import StringVar from tkinter import * from random import randint f1 = ("MS S
import tkinter as tk
from tkinter import StringVar
from tkinter import *
from random import randint
f1 = ("MS Serif", 29)
lives = 3
multiplier = 0
levelCounter = 1
answer2Counter = 0
answer3Counter = 1
answer4Counter = 2
def randomPosition(): #PLACES THE BUTTONS IN RANDOM VARIETY EACH TIME GAME IS OPEN
global xpos1, xpos2, ypos3, ypos4, random
random = randint(1,4)
print("Random Integer Position: " + str(random))
#CO-ORDINATES FOR BUTTON PLACEMENT
if random == 1:
xpos1 = 50
xpos2 = 730
ypos3 = 610
ypos4 = 720
elif random == 2:
xpos1 = 730
xpos2 = 50
ypos3 = 610
ypos4 = 720
elif random == 3:
xpos1 = 50
xpos2 = 730
ypos3 = 720
ypos4 = 610
elif random == 4:
xpos1 = 730
xpos2 = 50
ypos3 = 720
ypos4 = 610
randomPosition()
BtnWidth = 20
def wrong(self, controller):
global lives
lives -= 1
print("Lives: " + str(lives))
if lives == 0:
app.destroy()
def correct(self, controller):
global multiplier, levelCounter
multiplier += 1
levelCounter += 1
answer1.set(ftvArray[levelCounter])
answer2.set(wrongFtvArray[multiplier][answer2Counter]) #READS 1ST VALUE OF ARRAY
answer3.set(wrongFtvArray[multiplier][answer3Counter]) #READS 2ND VALUE OF ARRAY
answer4.set(wrongFtvArray[multiplier][answer4Counter]) #READS 3RD VALUE OF ARRAY
ftvArray = ['CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT', 'CORRECT']
wrongFtvArray = [['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG'], ['WRONG', 'WRONG', 'WRONG']] #'WRONG' IS A PLACEHOLDER VALUE
class Game(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frame = {}
for F in (FilmTV, LoadingScreen):
frame = F(container, self)
self.frame[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(FilmTV)
def show_frame(self, cont):
frame = self.frame[cont]
frame.tkraise()
class FilmTV(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
global answer1, answer2, answer3, answer4
answer1 = StringVar()
answer1.set(ftvArray[levelCounter])
answer2 = StringVar()
answer2.set(wrongFtvArray[multiplier][answer2Counter])
answer3 = StringVar()
answer3.set(wrongFtvArray[multiplier][answer3Counter])
answer4 = StringVar()
answer4.set(wrongFtvArray[multiplier][answer4Counter])
#ANSWERS
Btn = tk.Button(self, textvariable = answer1, command = lambda:[correct(self, controller), loading(self, controller), randomPosition()], font = f1, width = BtnWidth).place(x = xpos1, y = ypos3)
Btn2 = tk.Button(self, textvariable = answer2, command = lambda:[wrong(self, controller)], font = f1, width = BtnWidth).place(x = xpos1, y = ypos4)
Btn3 = tk.Button(self, textvariable = answer3, command = lambda:[wrong(self, controller)], font = f1, width = BtnWidth).place(x = xpos2, y = ypos3)
Btn4 = tk.Button(self, textvariable = answer4, command = lambda:[wrong(self, controller)], font = f1, width = BtnWidth).place(x = xpos2, y = ypos4)
def loading(self, controller):
self.controller.show_frame(LoadingScreen)
app.after(1500, nextLevel)
def nextLevel():
self.controller.show_frame(FilmTV)
class LoadingScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
LoadingLbl = tk.Label(self, text = 'CORRECT ANSWER! LOADING...', font = f1).place(x = 50, y = 50)
app = Game()
app.geometry("1260x830+150+5")
app.mainloop()
“单击后更新按钮的位置?(Tkinter Python GUI)”
下面是一个简单的例子:
是否要在放置按钮后更改按钮位置?你需要忘记按钮,然后把它换掉。我的第一点建议是不要把这么多东西塞进一个lambda。使用真正的函数,这将使调试代码变得越来越容易。@T4rk1n:您不必先忘记按钮。@HJC:当然可以。无论何时您在小部件上进行
pack
、place
、或grid
,它都拥有该小部件,使之前对pack
、place
或grid
的调用无效。只需调用place
。你似乎已经知道怎么做了。试试看。做一点实验。
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def on_button_press(widget):
import random
widget_size = (widget.winfo_width(), widget.winfo_height())
window_size = (widget.winfo_toplevel().winfo_width(),
widget.winfo_toplevel().winfo_height())
random_x = random.randint(0, window_size[0] - widget_size[0])
random_y = random.randint(0, window_size[1] - widget_size[1])
widget.place(x=random_x, y=random_y)
def main():
root = tk.Tk()
button = tk.Button(root, text="Place Randomly!")
button['command'] = lambda w=button: on_button_press(w)
button.place(x=0, y=0)
tk.mainloop()
if __name__ == '__main__':
main()