python - Tkinter 按钮相互混合
问题描述
我不知道如何解释,但我有两个不同的页面,每个页面都有一定数量的按钮作为网格
但是当我选择一个单选按钮时,然后在它混合后选择另一个
这是制作网格的代码
import tkinter as tk
from tkinter import filedialog, Text
import os
import time, sys
from time import sleep
root = tk.Tk()
root.geometry('700x700')
def changecolo(i, j):
if not ((i == 0 and j == 0) or (i == 12 and j == 12)):
if mFrame.grid_slaves(row = i, column = j)[0].cget('bg') == 'black':
mFrame.grid_slaves(row = i, column = j)[0].configure(bg = 'white')
elif mFrame.grid_slaves(row = i, column = j)[0].cget('bg') == 'white':
mFrame.grid_slaves(row = i, column = j)[0].configure(bg = 'grey')
elif mFrame.grid_slaves(row = i, column = j)[0].cget('bg') == 'grey':
mFrame.grid_slaves(row = i, column = j)[0].configure(bg = 'black')
def setalg():
value = 6
def makegrid6():
mFrame.grid_forget()
makegrid66()
def makegrid66():
for i in range(6):
for j in range (6):
if i == 0 and j == 0:
grids6 = tk.Button(mFrame, bg='blue', text = (5 - i) + (5 - j), state = 'disabled', highlightcolor="black", highlightthickness=1, width = 11, height = 5)
grids6.grid(row = i, column = j)
elif i == 5 and j == 5:
grids6 = tk.Button(mFrame, bg='red', text = (5 - i) + (5 - j) , state = 'disabled',highlightcolor="black", highlightthickness=1, width = 11, height = 5)
grids6.grid(row = i, column = j)
elif (((i == 0 or i == 1 or i == 2) and (j == 2)) or (i == 4 and j == 1)):
grids6 = tk.Button(mFrame, bg='white', text = (5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width = 11, height = 5, state = 'disabled')
grids6.grid(row = i, column = j)
elif (i == 3 and j == 2):
grids6 = tk.Button(mFrame, bg='grey', text = (5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width = 11, height = 5, state = 'disabled')
grids6.grid(row = i, column = j)
else:
grids6 = tk.Button(mFrame, bg='black', text = (5 - i) + (5 - j), fg = 'white', highlightcolor="black", highlightthickness=1, width = 11, height = 5, state = 'disabled')
grids6.grid(row = i, column = j)
return mFrame.grid_slaves, i, j
def makegrid10():
mFrame.grid_forget()
makegrid100()
def makegrid100():
for i in range(13):
for j in range (13):
if i == 0 and j == 0:
grids10 = tk.Button(mFrame, bg='blue', text = (12 - i) + (12 - j), fg = 'white', state = 'disabled', highlightcolor="black", highlightthickness=1, width = 4, height = 2, command=lambda x = i, y = j: changecolo(x, y))
grids10.grid(row = i, column = j)
elif i == 12 and j == 12:
grids10 = tk.Button(mFrame, bg='red', text = (12 - i) + (12 - j), fg = 'white', state = 'disabled',highlightcolor="black", highlightthickness=1, width = 4, height = 2, command=lambda x = i, y = j: changecolo(x, y))
grids10.grid(row = i, column = j)
else:
grids10 = tk.Button(mFrame, bg='black', text = (12 - i) + (12 - j), fg = 'white', highlightcolor="black", highlightthickness=1, width = 4, height = 2, command=lambda x = i, y = j: changecolo(x, y))
grids10.grid(row = i, column = j)
return mFrame.grid_slaves, i, j
def play():
if diffVar.get() == "sim":
butons, l, k = makegrid66()
Greedy(butons, l, k)
else:
butons, l, k = makegrid100()
Greedy(butons, l, k)
def aStar():
print(1000010)
def Greedy(butons, k, l):
print(k, l)
def UCS():
print(99)
mFrame = tk.Frame(root, height = 500, width = 500, bg = "lightgrey")
mFrame.place(x= 10, y = 10)
diffVar = tk.StringVar()
dif = tk.Label(root, text = "Problem Difficulty", bg = "Lightgrey")
dif.place (x = 550, y = 30)
simp = tk.Radiobutton(root, text = "Simple", value = "sim", bg = "Lightgrey", variable = diffVar, command = makegrid6)
simp.place(x = 550, y = 60)
comp = tk.Radiobutton(root, text = "Complex", value = "com", bg = "Lightgrey", variable = diffVar, command = makegrid10)
comp.place(x = 550, y = 90)
algVar = tk.IntVar()
algVar.set(1)
dif = tk.Label(root, text = "Choose an Algorithm", bg = "Lightgrey")
dif.place (x = 550, y = 180)
alg1 = tk.Radiobutton(root, text = "A*", value = 1, bg = "Lightgrey", variable = algVar, command = setalg)
alg1.place(x = 550, y = 210)
alg2 = tk.Radiobutton(root, text = "Greedy", value = 2, bg = "Lightgrey", variable = algVar, command = setalg)
alg2.place(x = 550, y = 240)
alg3 = tk.Radiobutton(root, text = "UCS", value = 3, bg = "Lightgrey", variable = algVar, command = setalg)
alg3.place(x = 550, y = 270)
playbu = tk.Button(root, text = "Play", bg = "White", width = 15, command = play)
playbu.place(x = 10, y = 550)
nextbu = tk.Button(root, text = "Next", bg = "White", width = 15)
nextbu.place(x = 140, y = 550)
fasterbu = tk.Button(root, text = "Faster", bg = "White", width = 15)
fasterbu.place(x = 270, y = 550)
resetbu = tk.Button(root, text = "Reset", bg = "White", width = 15)
resetbu.place(x = 400, y = 550)
root.mainloop()
他们是否有任何可能的方式使按钮以与画布大小相同的方式调整自身大小,而无需我调整按钮的宽度和高度
解决方案
首先,我用它来删除带有所有按钮的旧框架并为新按钮创建新框架
def makegrid6():
global mFrame
mFrame.destroy()
mFrame = tk.Frame(root, height = 500, width = 500, bg = "lightgrey")
mFrame.place(x= 10, y = 10)
makegrid66()
但它有一个问题 - 新框架是在其他小部件之上创建的,因此被隐藏且不可用。
所以我创建xFrame
了我把它放在窗口mFrame
中xFrame
的所有时间,所以它不会移动到其他小部件之上
root = tk.Tk()
root.geometry('700x700')
# parent `root`
xFrame = tk.Frame(root, height = 500, width = 500, bg = "lightgrey")
xFrame.place(x= 10, y = 10)
# parent `xFrame`
mFrame = tk.Frame(xFrame, height = 500, width = 500, bg = "lightgrey")
mFrame.pack()
然后它会在后面显示新框架checkbox
和其他按钮。
放入我使用mFrame
而不是因为它不需要选项并且不会有其他小部件所以不需要设置.xFrame
pack()
place()
x,y
你说你mFrame
输入了Canvas
(但我在代码中没有看到)所以你可以使用Canvas
而不是xFrame
.
顺便说一句:建议不要在=
内部函数周围使用空格。
请参阅PEP 8 - Python 代码样式指南。
这里的代码xFrame
我也进行了更改makegrid100()
以使其更短且更具可读性。
import tkinter as tk
from tkinter import filedialog, Text
import os
import time, sys
from time import sleep
# --- functions ---
def changecolo(i, j):
if not ((i == 0 and j == 0) or (i == 12 and j == 12)):
if mFrame.grid_slaves(row=i, column=j)[0].cget('bg') == 'black':
mFrame.grid_slaves(row=i, column=j)[0].configure(bg='white')
elif mFrame.grid_slaves(row=i, column=j)[0].cget('bg') == 'white':
mFrame.grid_slaves(row=i, column=j)[0].configure(bg='grey')
elif mFrame.grid_slaves(row=i, column=j)[0].cget('bg') == 'grey':
mFrame.grid_slaves(row=i, column=j)[0].configure(bg='black')
def setalg():
value = 6
def makegrid6():
global mFrame # inform function to use external/global variable instead of locla one when it will assign value (`=`)
mFrame.destroy()
mFrame = tk.Frame(xFrame, height=500, width=500, bg="lightgrey")
mFrame.pack()
makegrid66()
def makegrid66():
for i in range(6):
for j in range (6):
if i == 0 and j == 0:
grids6=tk.Button(mFrame, bg='blue', text=(5 - i) + (5 - j), state='disabled', highlightcolor="black", highlightthickness=1, width=11, height=5)
grids6.grid(row=i, column=j)
elif i == 5 and j == 5:
grids6=tk.Button(mFrame, bg='red', text=(5 - i) + (5 - j) , state='disabled', highlightcolor="black", highlightthickness=1, width=11, height=5)
grids6.grid(row=i, column=j)
elif (((i == 0 or i == 1 or i == 2) and (j == 2)) or (i == 4 and j == 1)):
grids6=tk.Button(mFrame, bg='white', text=(5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width=11, height=5, state='disabled')
grids6.grid(row=i, column=j)
elif (i == 3 and j == 2):
grids6=tk.Button(mFrame, bg='grey', text=(5 - i) + (5 - j) , highlightcolor="black", highlightthickness=1, width=11, height=5, state='disabled')
grids6.grid(row=i, column=j)
else:
grids6=tk.Button(mFrame, bg='black', text=(5 - i) + (5 - j), fg='white', highlightcolor="black", highlightthickness=1, width=11, height=5, state='disabled')
grids6.grid(row=i, column=j)
return mFrame.grid_slaves, i, j
def makegrid10():
global mFrame # inform function to use external/global variable instead of locla one when it will assign value (`=`)
mFrame.destroy()
mFrame = tk.Frame(xFrame, height=500, width=500, bg="lightgrey")
mFrame.pack()
makegrid100()
def makegrid100():
for i in range(13):
for j in range (13):
if i == 0 and j == 0:
bg_color = 'blue'
state = 'disabled'
elif i == 12 and j == 12:
bg_color = 'red'
state = 'disabled'
else:
bg_color = 'black'
state = 'normal'
b = tk.Button(mFrame, bg=bg_color, text=(12-i)+(12-j), state=state, fg='white', highlightcolor="black", highlightthickness=1, width=4, height=2, command=lambda x=i, y=j: changecolo(x, y))
b.grid(row=i, column=j)
return mFrame.grid_slaves, i, j
def play():
if diffVar.get() == "sim":
butons, l, k=makegrid66()
Greedy(butons, l, k)
else:
butons, l, k=makegrid100()
Greedy(butons, l, k)
def aStar():
print(1000010)
def Greedy(butons, k, l):
for i in range(k + 1):
for j in range(l + 1):
print(i, j)
mini=butons(row=i, column=j)[0].cget('text')
m, n=i, j
if i - 1 > -1:
if butons(row=i - 1, column=j)[0].cget('text') < mini:
mini=butons(row=i - 1, column=j)[0].cget('text')
m=i - 1
if i + 1 < 6:
if butons(row=i + 1, column=j)[0].cget('text') < mini:
mini=butons(row=i + 1, column=j)[0].cget('text')
m=i + 1
if j - 1 > -1:
if butons(row=i, column=j - 1)[0].cget('text') < mini:
mini=butons(row=i, column=j - 1)[0].cget('text')
n=j - 1
if j + 1 < 6:
if butons(row=i, column=j + 1)[0].cget('text') < mini:
mini=butons(row=i, column=j + 1)[0].cget('text')
n=j + 1
i, j=m, n
butons(row=m, column=n)[0].configure(bg='brown')
def UCS():
print(99)
# --- main ---
root = tk.Tk()
root.geometry('700x700')
xFrame = tk.Frame(root, height=500, width=500, bg="lightgrey")
xFrame.place(x= 10, y=10)
mFrame = tk.Frame(xFrame, height=500, width=500, bg="lightgrey")
mFrame.pack()
diffVar = tk.StringVar()
dif = tk.Label(root, text="Problem Difficulty", bg="Lightgrey")
dif.place (x=550, y=30)
simp = tk.Radiobutton(root, text="Simple", value="sim", bg="Lightgrey", variable=diffVar, command=makegrid6)
simp.place(x=550, y=60)
comp = tk.Radiobutton(root, text="Complex", value="com", bg="Lightgrey", variable=diffVar, command=makegrid10)
comp.place(x=550, y=90)
algVar = tk.IntVar()
algVar.set(1)
dif = tk.Label(root, text="Choose an Algorithm", bg="Lightgrey")
dif.place (x=550, y=180)
alg1 = tk.Radiobutton(root, text="A*", value=1, bg="Lightgrey", variable=algVar, command=setalg)
alg1.place(x=550, y=210)
alg2 = tk.Radiobutton(root, text="Greedy", value=2, bg="Lightgrey", variable=algVar, command=setalg)
alg2.place(x=550, y=240)
alg3 = tk.Radiobutton(root, text="UCS", value=3, bg="Lightgrey", variable=algVar, command=setalg)
alg3.place(x=550, y=270)
f2 = tk.Canvas(root, height=30, width=30, bg="White")
playbu = tk.Button(root, text="Play", bg="White", width=15, command=play)
playbu.place(x=10, y=550)
nextbu = tk.Button(root, text="Next", bg="White", width=15)
nextbu.place(x=140, y=550)
fasterbu = tk.Button(root, text="Faster", bg="White", width=15)
fasterbu.place(x=270, y=550)
resetbu = tk.Button(root, text="Reset", bg="White", width=15)
resetbu.place(x=400, y=550)
root.mainloop()
推荐阅读
- php - 如何使用 php 获取 FEDEX_ONE_RATE API
- excel - 如何替换 Pandas 数据框列中的部分文本?
- symfony - 为 API 定义一个特定的主机
- angular - PAYEE_ACCOUNT_RESTRICTED:商户账户受限
- node.js - 在 Tensorflow 上对图像进行切片的正确方法是什么
- tensorflow - 在 Tensorflow 2 中定义自己的 pad
- sql - 输出具有最新记录且没有空值 Oracle 的单行
- python - 无法将文件写入本地主机和网络共享
- c# - 无法从 Visual Studio 2019 中的解决方案资源管理器中删除文件
- python - 从内存中永久删除精灵 Pygame