基本上,當我想用??特定按鈕打開特定鏈接時,它不起作用。當您單擊第二個按鈕時,它會打開函式內的所有鏈接。
from tkinter import *
import webbrowser
root = Tk()
root.title("links")
root.geometry("700x500")
root.config(bg="#3062C7")
def links_unit1():
global vid_1, vid_2
bg_v1 = Canvas(root, bg="#3062C7", width=700, height=500)
bg_v1.place(x=0, y=0)
vid_1 = Button(root, text="Virtual memory", command=all_vids1)
vid_1.place(x=20, y=20)
vid_2 = Button(root, text="lossy, lossless", command=all_vids1)
vid_2.place(x=30, y=50)
def all_vids1():
if vid_1:
webbrowser.open("https://youtu.be/AMj4A1EBTTY")
elif vid_2:
webbrowser.open("https://youtu.be/v1u-vY6NEmM")
vid_unit1 = Button(root, text="Unit 1", command=links_unit1)
vid_unit1.place(x=10, y=10)
root.mainloop()
uj5u.com熱心網友回復:
你不能通過檢查 and 的值來做到這一點,vid_1因為vid_2它們總是真實的。相反,您可以通過使用選項的lambda運算式“即時”創建匿名函式,如下所示:command=Button
from tkinter import *
import webbrowser
root = Tk()
root.title("links")
root.geometry("700x500")
root.config(bg="#3062C7")
def links_unit1():
global vid_1, vid_2
bg_v1 = Canvas(root, bg="#3062C7", width=700, height=500)
bg_v1.place(x=0, y=0)
vid_1 = Button(root, text="Virtual memory",
command=lambda: webbrowser.open("https://youtu.be/AMj4A1EBTTY"))
vid_1.place(x=20, y=20)
vid_2 = Button(root, text="Lossy, Lossless",
command=lambda: webbrowser.open("https://youtu.be/v1u-vY6NEmM"))
vid_2.place(x=30, y=50)
vid_unit1 = Button(root, text="Unit 1", command=links_unit1)
vid_unit1.place(x=10, y=10)
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443945.html
標籤:Python tkinter 小部件 tkinter 按钮
下一篇:(Python)嘗試撰寫一個函式來讀取一個資料幀(從excel),然后在應用程式加載時將相同數量的行回圈到復選框中
