使用tkinter中的canvas进行图形结果

时间:2015-04-09 05:08:12

标签: python graph tkinter

我编写了我的代码,我想绘制每个骰子滚动的次数。这不起作用我怎么能这样做。我不想使用任何图形包。

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()

我该怎么办?我想要这个图,我正在做的应该工作。我不明白。

1 个答案:

答案 0 :(得分:0)

注意:总是写清楚是什么问题。在这里,我失去了一些时间来发现它是什么。 所以你的第一个reectangle有y坐标360和a。而且总是很小。 尝试类似:

    canvas = Canvas(root, width = 640, height = 100, bg = "red")    
    canvas.create_rectangle(1, 100, 50,100 - percent_1 *100, fill = "black")