Displaying a function in GUI - python

I am trying to make a "command generator" for a game where the user can input numbers into text boxes and my program will generate the command for them. I have this working almost perfectly, but the generated command is printed to the console, I want it to input to the GUI.
Sorry if my code is messy or not done the most efficient way, I am brand new to programming.
from tkinter import *
#Create window that is 500x500
window = Tk()
window.geometry("500x500")
#This is the final command that is outputted
def save():
print("/" + var2.get() + " " + entryx1.get() + " " + entryy1.get() + " " + entryz1.get() + " " + entryx2.get() + " " + entryy2.get()
+ " " + entryz2.get() + " " + var1.get())
#This is what you call when you want to display the dropdown input
var2 = StringVar()
#Creating the dropdown
drop2 = OptionMenu(window,var2,"fill","setblock")
drop2.configure(font=("Arial",10))
#This makes the text that displays in the window
coordx1 = Label(window,text="First X Coordinate: ")
coordy1 = Label(window,text="First Y Coordinate: ")
coordz1 = Label(window,text="First Z Coordinate: ")
coordx2 = Label(window,text="Second X Coordinate: ")
coordy2 = Label(window,text="Second Y Coordinate: ")
coordz2 = Label(window,text="Second Z Coordinate: ")
result = Label(window,text=save)
#This makes the text boxes that the user types in
entryx1 = Entry(window)
entryy1 = Entry(window)
entryz1 = Entry(window)
entryx2 = Entry(window)
entryy2 = Entry(window)
entryz2 = Entry(window)
#This is the submit button
submit = Button(window,text="Submit",command=save)
#Second dropdown variable
var1=StringVar()
#Creation of second dropdown list
drop1 = OptionMenu(window,var1,"destroy","hollow","keep","outline","replace")
drop1.configure(font=("Arial",10))
dropdownLabel = Label(window,text="Selector: ")
dropdownLabel2 = Label(window,text="Command: ")
#This says what goes where. 'E' and 'W' represent East and West
coordx1.grid(row=1,sticky=E)
coordy1.grid(row=2,sticky=E)
coordz1.grid(row=3,sticky=E)
coordx2.grid(row=4,sticky=E)
coordy2.grid(row=5,sticky=E)
coordz2.grid(row=6,sticky=E)
entryx1.grid(row=1,column=1)
entryy1.grid(row=2,column=1)
entryz1.grid(row=3,column=1)
entryx2.grid(row=4,column=1)
entryy2.grid(row=5,column=1)
entryz2.grid(row=6,column=1)
dropdownLabel.grid(row=7,column=0,sticky=E)
dropdownLabel2.grid(row=0,column=0,sticky=E)
drop1.grid(row=7,column=1,sticky=W)
drop2.grid(row=0,column=1,sticky=W)
submit.grid(row=8,columnspan=2,sticky=E)
mainloop()
To be clear, I want the function save() to be printed onto the GUI

First, you didn't grid your result Label.
Secondly, you can change your save() function to modify the result text everytime.
from tkinter import *
#Create window that is 500x500
window = Tk()
window.geometry("500x500")
#This is the final command that is outputted
def save():
result.config(text="/" + var2.get() + " " + entryx1.get() + " " + entryy1.get() + " " + entryz1.get() + " " + entryx2.get() + " " + entryy2.get()+ " " + entryz2.get() + " " + var1.get())
#This is what you call when you want to display the dropdown input
var2 = StringVar()
#Creating the dropdown
drop2 = OptionMenu(window,var2,"fill","setblock")
drop2.configure(font=("Arial",10))
#This makes the text that displays in the window
coordx1 = Label(window,text="First X Coordinate: ")
coordy1 = Label(window,text="First Y Coordinate: ")
coordz1 = Label(window,text="First Z Coordinate: ")
coordx2 = Label(window,text="Second X Coordinate: ")
coordy2 = Label(window,text="Second Y Coordinate: ")
coordz2 = Label(window,text="Second Z Coordinate: ")
result = Label(window,text=save)
#This makes the text boxes that the user types in
entryx1 = Entry(window)
entryy1 = Entry(window)
entryz1 = Entry(window)
entryx2 = Entry(window)
entryy2 = Entry(window)
entryz2 = Entry(window)
#This is the submit button
submit = Button(window,text="Submit",command=save)
#Second dropdown variable
var1=StringVar()
#Creation of second dropdown list
drop1 = OptionMenu(window,var1,"destroy","hollow","keep","outline","replace")
drop1.configure(font=("Arial",10))
dropdownLabel = Label(window,text="Selector: ")
dropdownLabel2 = Label(window,text="Command: ")
#This says what goes where. 'E' and 'W' represent East and West
coordx1.grid(row=1,sticky=E)
coordy1.grid(row=2,sticky=E)
coordz1.grid(row=3,sticky=E)
coordx2.grid(row=4,sticky=E)
coordy2.grid(row=5,sticky=E)
coordz2.grid(row=6,sticky=E)
entryx1.grid(row=1,column=1)
entryy1.grid(row=2,column=1)
entryz1.grid(row=3,column=1)
entryx2.grid(row=4,column=1)
entryy2.grid(row=5,column=1)
entryz2.grid(row=6,column=1)
dropdownLabel.grid(row=7,column=0,sticky=E)
dropdownLabel2.grid(row=0,column=0,sticky=E)
drop1.grid(row=7,column=1,sticky=W)
drop2.grid(row=0,column=1,sticky=W)
submit.grid(row=8,columnspan=2,sticky=E)
result.grid(row=9,columnspan=1)
window.mainloop()
This should do the trick.

Related

Tkinter freeze and won't load the result on screen

I'm new to programming and I'm trying to make an app that calculates the smallest common for you, but for some reason whenever i run it, tkinter seems to just freeze and i don't know what the error seems to be. I suspect that it's the myLabel part, since I can still read the result inside the Terminal.
Thanks in advance~
from tkinter import *
root = Tk()
root.title("I can find the smallest common, unless you enter letters... I'm dyslexic.")
numbers_list = []
global numbers
numbers = Entry(root, width = 10, borderwidth = 5, state = DISABLED)
numbers.pack()
numbers.insert(0, "")
#
def button_click():
#each gets a button
get_smallest_common = Button(root, text = 'Confirm your number!'
, command = smallest_common)
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Undo!'
, command = lambda: undo())
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Start the search!'
, command = lambda: find_the_s_common())
get_smallest_common.pack()
#disable the start button
def switch():
myButton['state'] = DISABLED
#configure helps bringing a disabled thing back to normal state
numbers.configure(state = "normal")
def smallest_common():
#add to the list for late use
numbers_list.append(numbers.get())
print(numbers_list)
numbers.delete(0, END)
def undo():
#add to the list for late use
numbers_list.pop()
print(numbers_list)
numbers.delete(0, END)
def find_the_s_common():
process_list = []
condition = True
x = 0
while condition:
#the multiplication keep rising till count is 3
a = int(x) + 1
x = a
#loop to multiply the number with x + 1
for number in numbers_list:
y = int(number) * int(a)
process_list.append(y)
#check whether the result has been added to append into list
if y in process_list:
#check whether the list contains two duplicates to
if process_list.count(y) == 3:
condition = False
result = 'The number is ' + str(y) + '!'
print(result)
else:
continue
else:
continue
myLabel = Label(root, text = result)
myLabel.pack()
#combine the two function for myButton
def button_click_switch():
button_click()
switch()
myButton = Button(root, text = 'Click me to start'
, command = lambda: [button_click(), switch()])
myButton.pack()
root.mainloop()
Most probably, you have problem if the numbers entered is less than 3. As a result, one simple way is to decrease the condition to 2 and if a single number is entered the result is the number itself, if 2 or more no freezing.
NOTE: Freezing here is actually, while loop is running as condition is always True, because of process_list.count(y) == 3 will always be False if less than 3 number entered.
See my suggestion:
from tkinter import *
root = Tk()
root.title("I can find the smallest common, unless you enter letters... I'm dyslexic.")
numbers_list = []
global numbers
numbers = Entry(root, width = 10, borderwidth = 5, state = DISABLED)
numbers.pack()
numbers.insert(0, "")
#
def button_click():
#each gets a button
get_smallest_common = Button(root, text = 'Confirm your number!', command = smallest_common)
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Undo!', command = lambda: undo())
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Start the search!' , command = lambda: find_the_s_common())
get_smallest_common.pack()
#disable the start button
def switch():
myButton['state'] = DISABLED
#configure helps bringing a disabled thing back to normal state
numbers.configure(state = "normal")
def smallest_common():
#add to the list for late use
numbers_list.append(numbers.get())
print(numbers_list)
numbers.delete(0, END)
def undo():
#add to the list for late use
numbers_list.pop()
print(numbers_list)
numbers.delete(0, END)
def find_the_s_common():
process_list = []
condition = True
x = 0
if len(numbers_list) == 1: # RETURN IF THE LIST HAS A SINGLE VALUE
result = 'The number is ' + str(numbers_list[0]) + '!'
else:
while condition:
#the multiplication keep rising till count is 3
a = int(x) + 1
x = a
#loop to multiply the number with x + 1
for number in numbers_list:
y = int(number) * int(a)
process_list.append(y)
#check whether the result has been added to append into list
if y in process_list:
#check whether the list contains two duplicates to
if process_list.count(y) == 2: # DECREASE THE CONDITIONS TO 2
condition = False
result = 'The number is ' + str(y) + '!'
print(result)
else:
continue
else:
continue
myLabel = Label(root, text = result)
myLabel.pack()
#combine the two function for myButton
def button_click_switch():
button_click()
switch()
myButton = Button(root, text = 'Click me to start', command = lambda: [button_click(), switch()])
myButton.pack()
root.mainloop()
Suggestion to add two conditons;
(1) create a condition until at least 1 number is entered! if len(numbers_list) == 0, loop to enter a number.
(2) create a condition, until N number is added, let's say user must enter at least 3 numbers, change process_list.count(y) == 3 to process_list.count(y) == N and add condition if len(numbers_list) != N, loop to enter more numbers.

Why is last button/task removed in this code?

I have a block of code which is called on a button press. The aim is to remove the task the button represents, and update all the other buttons to reflect the time changes. The function take an argument pos which is the position in the list of tasks to remove. That task entry is removed and the remaining task times updated.
The buttons are all removed and re-placed with new times and positions etc.
def remove_task(self, pos, *args):
print("\nRemoving task...")
print("List size = " + str(len(self.scheduled_tasks)))
print("Task pos = " + str(pos))
print("Task name = " + str(self.scheduled_tasks[pos].name))
del self.scheduled_tasks[pos] #Task deleted from list
print("Task deleted!")
print("Tasks:")
for task in self.scheduled_tasks:
print(task.name)
print("Re-assigning task n values...")
for index, tasks in enumerate(self.scheduled_tasks):
print("Index = " + str(index))
self.scheduled_tasks[index].n = int(index)
print("Name: " + str(self.scheduled_tasks[index].name))
print("N = " + str(self.scheduled_tasks[index].n))
print("N assingments complete")
print("List size = " + str(len(self.scheduled_tasks)))
for i in self.scheduled_tasks:
print("Task " + str(i.n) + " = " + str(i.name))
print("Deleting buttons...")
print("Number of buttons = " + str(len(self.ids.schedule.children)))
self.ids.schedule.clear_widgets()
print("Buttons deleted!")
position = 0
for i in self.scheduled_tasks:
print("Creating new buttons...")
if i.n == 0:
print("First button")
print("I.n = "+ str(i.n))
task_name = self.scheduled_tasks[i.n].name
print("Name = " + task_name)
task_time = self.scheduled_tasks[i.n].size
print("Task time = " + str(task_time))
parent_size = self.day
initial = App.get_running_app().user_start_time
end = initial + task_time
print("Task end time = " + str(end))
self.scheduled_tasks[i.n].set_start_time(initial)
self.scheduled_tasks[i.n].set_end_time(end)
print("New times set!")
if self.scheduled_tasks[i.n].class_type == "task":
print("Finding task size...")
self.task_size = self.scheduled_tasks[i.n].decimal_size
print("Size = " + str(self.task_size))
button_size = 1/int(parent_size/self.task_size)
print("Button_size = " + str(button_size))
task_type = self.scheduled_tasks[i.n].type
self.rgba = set_task_color(task_type, self.task_name).split(",")
if self.scheduled_tasks[i.n].class_type == "break":
button_size = 1/(self.parent_size/(self.break_size_init/60))
self.rgba = [0.5,0.5,0.5,0.5]
print("Position = " + str(position))
self.scheduled_tasks[i.n].n = position
position = position + 1
print("Position in scheduled tasks = " + str(self.scheduled_tasks[i.n].n))
task_button = Button(text = task_name + "\n" + str(self.scheduled_tasks[i.n].start_time.strftime("%H:%M")) + "\n - \n" + str(self.scheduled_tasks[i.n].end_time.strftime("%H:%M")),
size_hint = (button_size, 0.5),
text_size = (None, None),
font_size = 12,
pos_hint = {"top": 1},
background_normal = "white",
background_color = self.rgba)
task_button.text_size = task_button.size
task_button.halign = "center"
task_button.n = self.scheduled_tasks[i.n].n
print("Button position = " + str(task_button.n))
task_button.bind(on_release = lambda x: self.ids.schedule.remove_widget(task_button))
if self.scheduled_tasks[i.n].class_type == "task":
task_button.bind(on_release = lambda x: self.remove_task(i.n))
elif self.scheduled_tasks[i.n].class_type == "break":
task_button.bind(on_release = lambda x: self.remove_break(i.n))
print("Button functions bound")
self.ids.schedule.add_widget(task_button)
else:
print("Subsequent button")
print("I = " + str(i.n))
task_name = self.scheduled_tasks[i.n].name
print("Name = " + task_name)
print("Calculating times...")
initial = self.scheduled_tasks[i.n-1].end_time
end = initial + self.scheduled_tasks[i.n].size
print("Assigning times...")
self.scheduled_tasks[i.n].set_start_time(initial)
self.scheduled_tasks[i.n].set_end_time(end)
print("New times set!")
if self.scheduled_tasks[i.n].class_type == "task":
c.execute("SELECT Size FROM Tasks WHERE Name = ?;", (self.task_name,))
task_size_raw = "".join(c for c in str(str(c.fetchall())) if c not in "[](''`),")
self.task_size = float(task_size_raw) #hours, decimal
button_size = 1/int(parent_size/self.task_size)
task_type = self.scheduled_tasks[i.n].type
self.rgba = set_task_color(task_type, self.task_name).split(",")
if self.scheduled_tasks[i.n].class_type == "break":
button_size = 1/(self.parent_size/(self.break_size_init/60))
self.rgba = [0.5,0.5,0.5,0.5]
position = position + 1
print("position = " + str(self.scheduled_tasks[i.n].n))
task_button = Button(text = task_name + "\n" + str(self.scheduled_tasks[i.n].start_time.strftime("%H:%M")) + "\n - \n" + str(self.scheduled_tasks[i.n].end_time.strftime("%H:%M")),
size_hint = (button_size, 0.5),
text_size = (None, None),
font_size = 12,
pos_hint = {"top": 1},
background_normal = "white",
background_color = self.rgba)
task_button.text_size = task_button.size
task_button.halign = "center"
task_button.n = i.n
print("Button position = " + str(task_button.n))
task_button.bind(on_release = lambda x: self.ids.schedule.remove_widget(task_button))
if self.scheduled_tasks[i.n].class_type == "task":
task_button.bind(on_release = lambda x: self.remove_task(i.n))
elif self.scheduled_tasks[i.n].class_type == "break":
task_button.bind(on_release = lambda x: self.remove_break(i.n))
print("Button functions bound")
self.ids.schedule.add_widget(task_button)
print("Done!" + "\n")
print("Buttons created!")
print("Re-ordering complete!" + "\n")
Removing a task in the first instance works but when removing any later tasks/buttons the last task is always removed. The code output shows the button function is correctly bound to call the correct position argument but then always removes the last button/last entry in the list of tasks.
Output:
Removing task...
List size = 3
Task pos = 1 #Correct position
Task name = Test2
Task deleted!
Tasks:
Test
Test3
Re-assigning task n values...
Index = 0
Name: Test
N = 0
Index = 1
Name: Test3
N = 1
N assingments complete
List size = 2
Task 0 = Test
Task 1 = Test3
Deleting buttons...
Number of buttons = 3
Buttons deleted!
Creating new buttons...
First button
I.n = 0
Name = Test
Task time = 2:00:00
Task end time = 2021-01-03 16:30:00
New times set!
Finding task size...
Size = 2.0
Button_size = 0.5
0.8943980039499027,0.2144472118229821,0.011421795014607938,0.7
Position = 0
Position in scheduled tasks = 0
Button position = 0
Button functions bound
Creating new buttons...
Subsequent button
I = 1
Name = Test3
Calculating times...
Assigning times...
New times set!
0.8943980039499027,0.2144472118229821,0.011421795014607938,0.7
position = 1
Button position = 1
Button functions bound
Done!
Buttons created!
Re-ordering complete!
#^First removal, works fine with no issues^
Removing task...
List size = 2
Task pos = 1 #I clicked the button for the first task (Task 0) and this position argument was given!
Task name = Test3
Task deleted!
Tasks:
Test
Re-assigning task n values...
Index = 0
Name: Test
N = 0
N assingments complete
List size = 1
Task 0 = Test
Deleting buttons...
Number of buttons = 2
Buttons deleted!
Creating new buttons...
First button
I.n = 0
Name = Test
Task time = 2:00:00
Task end time = 2021-01-03 16:30:00
New times set!
Finding task size...
Size = 2.0
Button_size = 0.5
0.8943980039499027,0.2144472118229821,0.011421795014607938,0.7
Position = 0
Position in scheduled tasks = 0
Button position = 0
Button functions bound
Buttons created!
Re-ordering complete!
Removing task...
List size = 1
Task pos = 0
Task name = Test
Task deleted!
Tasks:
Re-assigning task n values...
N assingments complete
List size = 0
Deleting buttons...
Number of buttons = 1
Buttons deleted!
Buttons created!
Re-ordering complete!
#^Second and all subsequent removals remove the last task^
Can anybody see why the button is calling the last task instance to be removed? Why does every button after one task is correctly removed call for the last task to be removed?
I appreciate this is a long and detailed question so thanks in advance for any of your time!
I fixed this by binding a partial function instead of lambda to call the function on the button press. So instead of lambda x: remove_task(i.n) it would be partial(remove_task,i.n)

Does anyone know how to combine these functions?

I want to combine the functions def clicked and def clicked2 because they are the same but I don't know how to do that. I haven't tried yet but I can't think of a way to do it.
from tkinter import *
import random
window = Tk()
x = round(random.random()) + 1
m = 7
window.title("NIM-7 spel")
window.geometry('350x200')
lbl = Label(window, text="Player " + str(x) + " next")
lbl2 = Label(window, text="Their are " + str(m) + " coins left")
lbl.grid(column=0, row=1)
lbl2.grid(column=1, row=0)
def clicked():
global x
global m
m -= 1
if m < 1:
lol = "Player " + str(x) + " won!"
res = ""
else:
if x == 1:
x = 2
else:
x = 1
lol = "Their are " + str(m) + " coins left"
res = "Player " + str(x) + " is next"
lbl.configure(text=res)
lbl2.configure(text=lol)
def clicked2():
global x
global m
m -= 2
if m < 1:
lol = "Player " + str(x) + " won!"
res = ""
else:
if x == 1:
x = 2
else:
x = 1
lol = "Their are " + str(m) + " coins left"
res = "Player " + str(x) + " is next"
lbl.configure(text=res)
lbl2.configure(text=lol)
btn1 = Button(window, text="Take 1 coin", command=clicked)
btn2 = Button(window, text="Take 2 coins", command=clicked2)
btn1.grid(column=1, row=1)
btn2.grid(column=2, row=1)
window.mainloop()
Does anyone know how to do it?
You can add arguments to tkinter buttons if you use a lambda function.
Some identical questions were asked already:
Functions in Tkinter
How to call a function with arguments in "Button" function from "tkinter" python package?
def clicked(value):
global x
global m
m -= value
if m < 1:
lol = "Player " + str(x) + " won!"
res = ""
else:
if x == 1:
x = 2
else:
x = 1
lol = "Their are " + str(m) + " coins left"
res = "Player " + str(x) + " is next"
lbl.configure(text=res)
lbl2.configure(text=lol)
btn1 = Button(window, text="Take 1 coin", command = lambda:clicked(1))
btn2 = Button(window, text="Take 2 coins", command = lambda:clicked(2))
Pass in how much you want to decrement m as a parameter. So:
def clicked(decrement):
global x
global m
m -= decrement
if m < 1:
lol = "Player " + str(x) + " won!"
res = ""
else:
if x == 1:
x = 2
else:
x = 1
lol = "Their are " + str(m) + " coins left"
res = "Player " + str(x) + " is next"
lbl.configure(text=res)
lbl2.configure(text=lol)
Then call it like this: clicked(1) and clicked(2).
Because you are passing it as a parameter to Button, you can create dummy functions:
def clicked1():
clicked(1)
and similar for clicked2.

Adding a hint in my python Hangman game

I am new to IDLE Python,
My IDLE python Hangman game is a bit complex(contains different pictures and windows)
I want to add a one time use hint button in my hangman game.
The word is chosen from a txt file.
How do i get the random choice function to get working?
Here is the start game code :
def startgame():
global word
infile_name = getInfile()
# Choose a word at random from the acquired word list
word, word_len = chooseWord(infile_name)
if word_len < 1:
messagebox.showinfo("Error", "Please enter a word!")
startgame()
## lots of variables for actual game ##
global wordarray
wordarray = []
global guessedletters
guessedletters = []
i = 0
while i < word_len:
wordarray.append('_')
wordarray.append(' ')
i = i + 1
global correctcounter
correctcounter = 0
global incorrectcounter
incorrectcounter = 0
## end variables ##
game = Toplevel()
game.wm_title("Hangman")
game.minsize(100,100)
game.geometry("500x450")
man = PhotoImage(file="gallows.gif")
hiddenword = StringVar()
gamelabel1 = Label(game, image=man)
gamelabel1.image = man
gamelabel1.pack()
gamelabel2 = Label(game, textvariable=hiddenword)
gamelabel2.pack()
guessfield = Entry(game)
guessfield.pack()
remainingguesses = 9 - incorrectcounter
wordprint = ''.join(wordarray) + "\n"
wordprint = wordprint + "Guessed Letters: " + ', '.join(guessedletters) + "\n"
wordprint = wordprint + "Incorrect Guesses Remaining: " + str(remainingguesses) + "\n"
hiddenword.set(wordprint)
bguessletter = Button(game, text="Guess Letter", width=15, command=lambda:
letterguess(guessfield, hiddenword, game, gamelabel1, man))
bguessletter.pack()
bguessword = Button(game, text="Guess Word [ONE CHANCE]", width=25, command=lambda:wordguess(guessfield, hiddenword, game, man))
bguessword.pack()
game.mainloop()
You want random choice? Then that seems like you want random.choice. See random choice.

Graph results using canvas in tkinter

I have written my code and I want to graph the amount of times each dice is rolled. This isn't working how can I do it. I don't want to use any graphing packages.
import random
import time
name="JORDAN SUMMERS RANDOM NUMBER GENERATOR ASSIGNMENT"
by="## Created by Jordan Summers ##"
date="# 31.03.2015"
symbol="_"*50
bos=""*5
warning = "Please enter a number, i.e. 1. Not 'one'."
information=""" YOU WILL CHOOSE HOW MANY TIMES TO ROLL THE DICE,
ONCE ROLLED THE PROGRAM WILL PRINT THE NUMBERS, THEN IT WILL SORT THE NUMBERS
HIGHEST TO LOWEST. """
print("{:^80}\n{:^80}\n{:^80}\n{:^80}\n{:^80}\n{}".format(name,by,date,warning,symbol,bos,))
rolllist = []
total = 0
dice = int(input("How many times do you want to roll the dice? "))
if dice >50:
print("Please enter a lower number")
else:
for i in range(dice):
rolllist.append (random.randint(1,6))
print ("You rolled a " + str(rolllist[i])) #Here we are adding the results to the list
total = sum(rolllist)
print ("The total of the numbers " + str(total)) #This is calculating the total
rolllist.sort()
print ("numbers sorted " + str(rolllist)) #This is sorting the numbers in order of value
a = rolllist.count(1)
print ("The number 1 occured " + str(a) + " times." + " It's percentage is ") #Line 34 to line 51 are counting how many times a number occured.
percent_1 = ((int(rolllist.count(1))/(dice)))
print(percent_1*100)
b = rolllist.count(2)
print ("The number 2 occured " + str(b) + " times." + " It's percentage is ")
percent_2 = ((int(rolllist.count(2))/(dice)))
print(percent_2*100)
c = rolllist.count(3)
print ("The number 3 occured " + str(c) + " times." + " It's percentage is ")
percent_3 = ((int(rolllist.count(3))/(dice)))
print(percent_3*100)
d = rolllist.count(4)
print ("The number 4 occured " + str(d) + " times." + " It's percentage is ")
percent_4 = ((int(rolllist.count(4))/(dice)))
print(percent_4*100)
e = rolllist.count(5)
print ("The number 5 occured " + str(e) + " times." + " It's percentage is ")
percent_5 = ((int(rolllist.count(5))/(dice)))
print(percent_5*100)
f = rolllist.count(6)
print ("The number 6 occured " + str(f) + " times." + " It's percentage is")
percent_6 = ((int(rolllist.count(6))/(dice)))
print(percent_6*100)
from tkinter import *
root = Tk()
canvas = Canvas(root, width = 640, height = 360, bg = "red")
canvas.pack()
canvas.create_rectangle(1, 360, 50,(a), fill = "black")
canvas.create_rectangle(60,360, 100,(b), fill = "blue")
canvas.create_rectangle(110,360, 150,(c), fill = "yellow")
canvas.create_rectangle(160,360, 200,(d), fill = "white")
canvas.create_rectangle(210,360, 250,(e), fill = "orange")
canvas.create_rectangle(260,360, 300,(f), fill = "pink")
root.mainloop()
What do I do. I want this to graph, what I'm doing should work. I don't understand.
Note: always write clearly what is the problem. Here, I lost some time finding what it was.
So your first reectangle has y-coordinates 360 and a. And a is always small.
Try something like:
canvas = Canvas(root, width = 640, height = 100, bg = "red")
canvas.create_rectangle(1, 100, 50,100 - percent_1 *100, fill = "black")

Categories

Resources