问题描述
import tkinter
随机导入
class App(tkinter.Frame):
def __init __(self,master):
tkinter.Frame .__ init __(self,master)
self.master = master
self.grid()
self.colors =(('blue','green','orange ','红','黄','谭','布朗','浅绿'))
self.bttn1 = tkinter.Button(self,text ='Click Me!', bg = str(self.colors [1]),
command = self.change_color)
self.bttn2 = tkinter.Button(self,text ='点击我! ',bg = str(self.colors [0]),
command = self.change_color)
self.bttn3 = tkinter.Button(self,text ='Click我!',bg = str(self.colors [1]),
command = self.change_color)
self.bttn4 = tkinter.Button(self,text = 点击我!',bg = str(self.colors [2]),
command = self.change_color)
self.bttn5 = tkinter.Button(self,text = '点击我!',bg = str(self.colors [3]),
command = self.change_color)
self.bttn1.grid(row = 2, column = 0,sticky ='nw')
self.bttn2.grid(row = 2,column = 1,sticky ='nw')
self.bttn3。 grid(row = 2,column = 2,sticky ='nw')
self.bttn4.grid(row = 2,column = 3,sticky ='nw')
self.bttn5.grid(row = 2,column = 4,sticky ='nw')
def change_color(self):
random.shuffle(colors)
如果self.bttn_clicks + 1:
self.bttn1.configure(background = color)
if self.bttn_clicks + 2:
self.bttn2.configure(background = color)
if self.bttn_clicks + 3:
bself.bttn3.configure(background = color)
self.bttn4.configure(background = color)
self.bttn5.configure(background = color)
root = tkinter.Tk()
root.title('颜色按钮')
root.geometry('300x50')
app = App(root)
root.mainloop()
我尝试过:
1.我试图使用标签和random.shuffle。
2.然后有人说尝试使用Iterators(我可能只是因为我是初学者而使用它错了。
3.现在我试图在函数下使用字符串。
我会即使它改变了一切,也要采取任何灵魂。我知道我是初学者,我正在做的事情甚至可能都没有帮助我。
import tkinter
import random
class App(tkinter.Frame):
def __init__(self, master):
tkinter.Frame.__init__(self,master)
self.master=master
self.grid()
self.colors=(('blue', 'green', 'orange', 'red', 'yellow', 'Tan', 'Brown', 'light green'))
self.bttn1=tkinter.Button(self, text='Click Me!', bg=str(self.colors[1]),
command=self.change_color)
self.bttn2=tkinter.Button(self, text='Click Me!', bg=str(self.colors[0]),
command=self.change_color)
self.bttn3=tkinter.Button(self, text='Click Me!', bg=str(self.colors[1]),
command=self.change_color)
self.bttn4=tkinter.Button(self, text='Click Me!', bg=str(self.colors[2]),
command=self.change_color)
self.bttn5=tkinter.Button(self, text='Click Me!', bg=str(self.colors[3]),
command=self.change_color)
self.bttn1.grid(row = 2, column = 0, sticky = 'nw')
self.bttn2.grid(row = 2, column = 1, sticky = 'nw')
self.bttn3.grid(row = 2, column = 2, sticky = 'nw')
self.bttn4.grid(row = 2, column = 3, sticky = 'nw')
self.bttn5.grid(row = 2, column = 4, sticky = 'nw')
def change_color(self):
random.shuffle(colors)
if self.bttn_clicks + 1:
self.bttn1.configure(background=color)
if self.bttn_clicks + 2:
self.bttn2.configure(background=color)
if self.bttn_clicks + 3:
bself.bttn3.configure(background=color)
self.bttn4.configure(background=color)
self.bttn5.configure(background=color)
root=tkinter.Tk()
root.title('Color Button')
root.geometry('300x50')
app=App(root)
root.mainloop()
What I have tried:
1. I tried to use labels and random.shuffle.
2. then someone said to try and use Iterators(I might just be using it wrong because I am a beginner)
3. Now Im trying to use a string under a function.
I will take any soulotion even if It changes everything. I understand that I am a beginner and what I'm doing might not even be helping me.
推荐答案
这篇关于如何为我的按钮洗牌这个颜色列表?每次程序启动时我都试图让颜色变得混乱。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!