import tkinter
import tkinter as tk
from tkinter import *
import turtle
from turtle import *
wn = turtle.Screen()
t = turtle.Turtle()
root = Tk()
root.title("control board")
turtle.title("drawing board")
root["bg"] = "black"
wn.bgcolor("black")
t.color("white")
root2 = tk.Toplevel(root)
root2.withdraw()
root3 = tk.Toplevel(root)
root3.withdraw()
root4 = tk.Toplevel(root)
root4.withdraw()
root5 = tk.Toplevel(root)
root5.withdraw()
def circle():
t.circle(50)
def line():
b7.pack()
b8.pack()
b9.pack()
b2.forget()
def l_line():
t.left(30)
def r_line():
t.right(30)
def check():
b5.config(text = "u sure?", command = doublecheck)
def doublecheck():
turtle.bye()
root.destroy()
def sf():
t.forward(10)
def bf():
t.forward(50)
def hidef():
b7.forget()
b8.forget()
b9.forget()
b19.forget()
b2.pack()
b19.pack()
def colors():
root3.deiconify
root3.title("theme window")
b11 = Button(root3, height=2,
width=20,
bg="BLUE", fg="white",
text="blue",
command=bluec, )
b12 = Button(root3, height=2,
width=20,
bg="red", fg="white",
text="red",
command=redc, )
b13 = Button(root3, height=2,
width=20,
bg="yellow", fg="white",
text="yellow",
command=yellowc, )
b22 = Button(root3, height=2,
width=20,
bg="black", fg="yellow",
text="close color window",
command=root3.destroy, )
b11.pack()
b12.pack()
b13.pack()
b22.pack()
def bluec():
t.color("blue")
def redc():
t.color("red")
def yellowc():
t.color("yellow")
def undo():
t.undo()
def themes():
root2.deiconify()
root2.title("theme window")
b16 = Button(root2, height=2,
width=20,
bg="yellow", fg="blue",
text="hannukah theme",
command=htheme, )
b17 = Button(root2, height=2,
width=20,
bg="white", fg="red",
text="christmas theme",
command=ctheme, )
b22 = Button(root2, height=2,
width=20,
bg="white", fg="red",
text="customize themes",
command=customthemehub)
b18 = Button(root2, height=2,
width=20,
bg="black", fg="yellow",
text="close theme window",
command= root2.destroy, )
b16.pack()
b17.pack()
b22.pack()
b18.pack()
def htheme():
wn.bgcolor("yellow")
t.color("blue")
root["bg"] = "yellow"
b1["bg"] = "blue"
b2["bg"] = "blue"
b3["bg"] = "blue"
b4["bg"] = "blue"
b5["bg"] = "blue"
b7["bg"] = "blue"
b8["bg"] = "blue"
b9["bg"] = "blue"
b14["bg"] = "blue"
b15["bg"] = "blue"
b1["fg"] = "yellow"
b2["fg"] = "yellow"
b3["fg"] = "yellow"
b4["fg"] = "yellow"
b5["fg"] = "yellow"
b7["fg"] = "yellow"
b8["fg"] = "yellow"
b9["fg"] = "yellow"
b14["fg"] = "yellow"
b15["fg"] = "yellow"
def ctheme():
wn.bgcolor("red")
t.color("white")
root["bg"] = "red"
b1["bg"] = "white"
b2["bg"] = "white"
b3["bg"] = "white"
b4["bg"] = "white"
b5["bg"] = "white"
b7["bg"] = "white"
b8["bg"] = "white"
b9["bg"] = "white"
b14["bg"] = "white"
b15["bg"] = "white"
b1["fg"] = "red"
b2["fg"] = "red"
b3["fg"] = "red"
b4["fg"] = "red"
b5["fg"] = "red"
b7["fg"] = "red"
b8["fg"] = "red"
b9["fg"] = "red"
b14["fg"] = "red"
b15["fg"] = "red"
def penswitch():
b20.pack(side = tkinter.BOTTOM)
b21.pack(side = tkinter.BOTTOM)
b19.forget()
def penu():
t.penup()
b20.forget()
b21.forget()
b19.pack(side = tkinter.TOP)
def pend():
t.pendown()
b20.forget()
b21.forget()
b19.pack()
def customthemehub():
root4.deiconify()
b23 = Button(root4, height=2,
width=20,
bg="black", fg="blue",
text="select background color",
command=bgselect, )
b10 = Button(root4, height=2,
width=20,
bg="black", fg="white",
text="select drawing color",
command=colors )
b10.pack()
b23.pack()
def bgselect():
root4.withdraw()
root5.deiconify()
b24 = Button(root5, height=2,
width=20,
bg="blue", fg="white",
text="blue background",
command=bluebg)
b25 = Button(root5, height=2,
width=20,
bg="yellow", fg="white",
text="yellow background",
command=yellowbg)
b24.pack()
b25.pack()
def bluebg():
wn.bgcolor("blue")
def yellowbg():
wn.bgcolor("yellow")
b1 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="circle",
command=circle,)
b2 = Button(root, height = 2,
width = 20,
bg = "black", fg = "white",
text="line",
command=line,)
b3 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text=" turn left",
command= l_line ,)
b4 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text=" turn right",
command= r_line ,)
b5 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text=" exit",
command= check ,)
b7 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="10 steps",
command= sf,)
b8 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="50 steps",
command= bf,)
b9 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="hide",
command= hidef,)
b14 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="undo last action",
command= undo,)
b15 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="select different themes",
command= themes,)
b19 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="pen up/down",
command= penswitch,)
b20 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="pen up",
command= penu,)
b21 = Button(root, height = 2,
width = 20,
bg="black", fg="white",
text="pen down",
command= pend,)
b15.pack()
b14.pack()
b5.pack(side = tkinter.LEFT, )
b4.pack()
b3.pack()
b1.pack()
b2.pack()
b19.pack()
root.mainloop()
抱歉煩人的代碼我知道它一團糟,基本上問題在于 def: bgselecr 和選擇顏色,如果有人可以運行此代碼,請單擊自定義主題然后自定義主題然后選擇背景顏色,當我關閉它時重新打開背景顏色的選項卡,它“復制”了我使用撤銷和去圖示化方法的按鈕,如果你需要,我可以重新解釋我的意思我不知道是什么原因造成的,任何幫助都會得到幫助
uj5u.com熱心網友回復:
所以你的代碼的問題是,對于Toplevel你已經初始化的任何 window( ),你最初是withdraw它,但是每當你deiconify再次初始化時,你都會給它打包兩個新按鈕。
所以以背景色選擇視窗為例,第一次運行該功能時bgselect(),它會將兩種顏色的兩個按鈕打包到視窗中以供選擇。現在,當您關閉視窗時,實際上該視窗以及兩個按鈕仍然存在,只是已被撤回。
這意味著下次您使用deiconify它時,舊的兩個打包按鈕仍然存在,無需再次打包兩個新按鈕。

現在,這個問題的解決方案是在初始化Toplevel小部件時添加這些按鈕。在檢查您的代碼時,似乎所有Toplevel小部件都以導致重復打包新按鈕的方式進行了配置。通過從函式中洗掉打包代碼并將其重定位到所有Toplevels 初始化的行,問題就解決了!

例如,root5它在修復后看起來像這樣 -:
def bluebg():
wn.bgcolor("blue")
def yellowbg():
wn.bgcolor("yellow")
root5 = tk.Toplevel(root)
# Buttons are initialized once.
b24 = Button(root5, height=2,
width=20,
bg="blue", fg="white",
text="blue background",
command=bluebg)
b25 = Button(root5, height=2,
width=20,
bg="yellow", fg="white",
text="yellow background",
command=yellowbg)
b24.pack()
b25.pack()
root5.withdraw()
def bgselect():
root4.withdraw()
root5.deiconify()
# Buttons are no longer repeatedly initialized.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358520.html
上一篇:如何獲取動態物件鍵并使用它?
