Problems assigning a value from a randomly populated array to a tkinter button
Tag : python , By : user185144
Date : March 29 2020, 07:55 AM
Does that help This may be what you're wanting to do. Borrowing info from this answer, I put a callback in the Button command to pass the row and column numbers. # previous code
amount_to_add='B'
my_array = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]]
# ... all code up to here unchanged ...
for _ in range(4):
my_array[random.randrange(len(my_array))][random.randrange(len(my_array))] = amount_to_add
Treasure='T'
for _ in range(4):
my_array[random.randrange(len(my_array))][random.randrange(len(my_array))] = Treasure
# print my_array # uncomment to check array values before shuffle
random.shuffle(my_array)
# print my_array # and after shuffle to confirm button is getting the correct values
def changelabel(j,k):
tkinter.messagebox.showinfo('Button %d,%d' % (j,k), 'Button %d,%d contains a %s' % (j,k, my_array[j][k]))
print('hello this button works')
# return # not needed
i = 0
bttn = []
for j in range(0,8):
for k in range(8):
bttn.append(Button(TreasureMap, text = ' ', command= lambda row = j, column = k: changelabel(row, column)))
bttn[i].grid(row = j, column = k)
i += 1
|
tkinter: use single button with multiple frames and functions
Date : March 29 2020, 07:55 AM
hop of those help? Iterating over frames[1:4] won't work very well here, as it leaves you with no easy way to get the next/previous frame. Try iterating over indices instead: for i in range(1,4):
nb = Button(frames[i], text="Next", command=frames[i+1].tkraise)
nb.pack(side=RIGHT)
pb = Button(frames[i], text="Previous", command=frames[i-1].tkraise)
pb.pack(side=LEFT)
for i in range(len(frames)):
if i != len(frames) - 1:
nb = Button(frames[i], text="Next", command=frames[i+1].tkraise)
nb.pack(side=RIGHT)
if i != 0:
pb = Button(frames[i], text="Previous", command=frames[i-1].tkraise)
pb.pack(side=LEFT)
|
Assigning functions to buttons created in a for loop in Tkinter
Tag : python , By : Priyatna Harun
Date : March 29 2020, 07:55 AM
hop of those help? I have a for loop which creates an amount of buttons (in tkinter) based on the length of my list, compkeys. When I make each button, it is given a previously made function which takes one input. I am trying to make the input of the function specific to the iteration of the for loop. For example, the first button that is created in the loop should have the first item in the list comp keys as input in its function. , You must pass the parameter through the lambda function: for x in range(len(compKeys)):
compButton = Button(root, text=compKeys[x], command=lambda z=compKeys[x]: compBuDef(z))
compButton.place(x=x*100+200, y=300)
for idx, ckey in enumerate(compKeys):
compButton = Button(root, text=ckey, command=lambda z=ckey: compBuDef(z))
compButton.place(x=idx*100+200, y=300)
|
Tkinter radio button assigning last variable when run?
Tag : python , By : Tamizhvendan
Date : November 01 2020, 04:01 AM
this will help I have three files and I am trying to change the value of a variable through radio buttons: , Consider this line of code: epochRadioButton20 = Radiobutton(..., command=changeEpoch(20))
result = changeEpoch(20)
epochRadioButton20 = Radiobutton(..., command=result)
def changeEpoch():
epochValue = var1.get()
config.epoch=epochValue
...
epochRadioButton20 = Radiobutton(..., command=lambda: changeEpoch)
epochRadioButton20 = Radiobutton(..., command=lambda: changeEpoch(20))
|
How can I connect multiple text field inputs along with their functions to one button in tkinter python?
Tag : python , By : user119413
Date : March 29 2020, 07:55 AM
Hope this helps I have this issue where I need to display other function's values using the only button which is Calculate in this case. The first function is displayed but the other function not while using its text input field. import tkinter as tk
from statistics import median, mode, stdev
class Calculator(tk.Frame):
"""Calculate mean median mode and standard deviation"""
def __init__(self, parent, *args, **kwargs):
#setup tkinter frame
tk.Frame.__init__(self, parent, *args, **kwargs)
parent = parent
self.makeWidgets()
def makeWidgets(self):
"""make and pack various widgets"""
self.meanLabel = tk.Label(self, text="")
self.medianLabel = tk.Label(self, text="")
self.modeLabel = tk.Label(self, text="")
self.sdLabel = tk.Label(self, text="")
self.entry = tk.Entry(self)
self.calcuateButton = tk.Button(self, text="Calculate!", command = self.calculate)
self.meanLabel.pack()
self.medianLabel.pack()
self.modeLabel.pack()
self.sdLabel.pack()
self.entry.pack()
self.calcuateButton.pack()
def calculate(self):
self.listOfNumbers = self.entry.get()
#get what is inputed into the entry widget
self.listOfNumbers = self.listOfNumbers.split(' ')
#split the input string by spaces
for ind, i in enumerate(self.listOfNumbers):
#convert each item in the list of strings of numbers to ints
self.listOfNumbers[ind] = int(i)
#calculate!
self.calcMean()
self.calcMedian()
self.calcMode()
self.calcSd()
def calcMean(self):
"""calculate the mean"""
mean = sum(self.listOfNumbers)/len(self.listOfNumbers)
self.meanLabel.config(text='mean = ' + str(mean))
return mean
def calcMedian(self):
"""calculate the median"""
med = median(self.listOfNumbers)
self.medianLabel.config(text='median = ' + str(med))
return med
def calcMode(self):
"""calculate the mode"""
Mode = max(set(self.listOfNumbers), key=self.listOfNumbers.count)
self.modeLabel.config(text='mode = ' + str(Mode))
return Mode
def calcSd(self):
"""calculate the standard deviation"""
sd = stdev(self.listOfNumbers)
self.sdLabel.config(text='Standard deviation = ' + str(sd))
return sd
if __name__ == '__main__':
root = tk.Tk()
root.title('calculate mean, median, mode and standard deviation')
root.geometry('400x200')
Calculator(root).pack(side="top", fill="both", expand=True)
root.mainloop()
|