ytac=[]
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self, title: str):
super().__init__()
self.title(title)
self.style = ttk.Style(self)
self.style.theme_use("classic")
self.frame0 = ttk.Frame(self, padding=(5,5))
self.frame1 = ttk.Frame(self, padding=(5,5))
self.frame2 = ttk.Frame(self, padding=(5,5))
self.frame3 = ttk.Frame(self, padding=(5,5))
self.initialize_frames()
self.initialize_widgets()
self.geometry("450x400")
def initialize_widgets(self):
self.uno_label.grid(row=0, column=0, sticky=tk.NSEW, padx=10, pady=10, columnspan=10)
def make_frame0(self):
self.coding_button = ttk.Button(self.frame0, text="Coding", command=self.get)
self.coding_button.grid(row=1, column=5)
def make_frame1(self):
self.gamedev_button = ttk.Button(self.frame1, text="Game Development", command=self.get)
self.gamedev_button.grid(row=1 ,column=5)
def make_frame2(self):
self.digitalmedia_button = ttk.Button(self.frame2, text="Digital Media", command=self.get)
self.digitalmedia_button.grid(row=1, column=5)
def make_frame3(self):
self.uno_label = ttk.Label(self, text="Hello fellow Ytacer or so they call 'it. Ytac is a 4 week journey that allows to \ntake a Stem class of your choosing from a not so wide range of classes. \nIt's also unfourtenetly not sponsored by Dr.Pepper(f in the chat). 'Are you ready \nto start your expedition? What's 'my name you 'ask? I have 'no name but you \ncan call me Took. 'And also, Mel 'sucks and 'shouldn't be 'appreciated \nthroughout the rest of the cohort. *ahem*, they're 3 pathways to choose from,\n Coding(1), Game Development(2), Digital Media(3). Choose from one of these \nby clicking on the respective button. But before you do that I would like for us \nto introduce our sponsors RAID SHADOW LEGE-.")
def initialize_frames(self):
self.make_frame0()
self.make_frame1()
self.make_frame2()
self.make_frame3()
self.frame0.grid(row=4, column=0)
self.frame1.grid(row=4, column=1)
self.frame2.grid(row=4, column=2)
def get(self):
if self.coding_button:
print("ok")
elif self.gamedev_button:
print("poyo")
elif self.digitalmedia_button:
print("GUH-HUH")
if __name__ == "__main__":
app = App("YTAC")
app.mainloop()
即使我按下了所有相應的按鈕,這個 def 函式也只會列印出第一個字串而不是其他兩個字串。

在我按下相應的按鈕后,預計會列印出相應的字串。我試圖將 elifs 更改為他們自己的 if 陳述句,但這會導致更多問題。
uj5u.com熱心網友回復:
在沒有看到所有代碼的情況下,我無法準確診斷您的問題,但這可能在于您使用 Tkinter 按鈕的方式。
另一個與您的問題相似的問題 To Detect Button Press In Python Tkinter Module
一個非常詳盡的 Tkinter 按鈕教程: https ://www.tutorialspoint.com/python/tk_button.htm
本質上,Tkinter 按鈕的定義如下:
buttonName = tkinter.Button(master, option1=value1, option2=value1...)
Tkinter 按鈕沒有“被按下”變數,而是設定在按下按鈕時運行的命令(一個函式)。
buttonName = tkinter.Button(master, command=function_name, option1=value1, option2=value2...)
注意:不要在函式名后面加上括號,因為這樣會運行函式。唯一的例外是如果您有一個回傳函式的函式
如果你想要一個在你按下按鈕時改變的全域變數,這樣的函式可以作業:
global_var = False
def callback():
global global_var
global_var = not global_var
button = tkinter.Button(master, command=callback)
在上面的示例中,替換master為 Tkinter 視窗的名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523439.html
