单击后更新按钮的位置? (Tkinter Python GUI)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单击后更新按钮的位置? (Tkinter Python GUI)相关的知识,希望对你有一定的参考价值。

启动游戏会使按钮按随机顺序(由功能引起)但当我继续按下“正确”按钮时,按钮的位置在加载屏幕后不会更新;即使他们的文字更新。即使在randomPosition函数改变每个按钮的位置后,按钮的位置也不会更新。有没有办法更新按钮的位置,以便加载屏幕后“正确”的位置不在同一个位置?

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)”

这是一个最小的例子:

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()

以上是关于单击后更新按钮的位置? (Tkinter Python GUI)的主要内容,如果未能解决你的问题,请参考以下文章

Tkinter - 单击默认情况下未设置的按钮后无法获取单选按钮值

单击按钮后如何将输入值(TKinter)传递给其他类中的函数并使用传递的值运行函数[重复]

使用 Tkinter 按下按钮后更新标签 [重复]

Python / Tkinter:使用按钮更新类变量

如何在没有按钮单击python tkinter的情况下进入另一个页面

按钮点击后,Python tkinter在框架中显示文本