Python Tkinter program crashing with Entry

时间:2016-10-20 12:59:32

标签: python user-interface tkinter calculator tkinter-entry

Been trying to create a simple gui calculator with tkinter, but as soon as I added e = Entry(root) e.pack() it crashes my program on startup? removing it makes my program run normally? very confused why it's doing it and have wondered if maybe its cause of how much code I have?

import sys
from tkinter import *
import webbrowser
import time

# window Defaults



root = Tk()
root.title("Calculator")
root.geometry("319x212")
root.configure(background="#3b3b3b")
root.iconbitmap("Calculator.ico")

e = Entry(root)
e.pack()




# Addition Numbers

total = 0

def plusOne():
    global total
    total += 1
    print(total)
def plusTwo():
    global total
    total += 2
    print(total)
def plusThree():
    global total
    total += 3
    print(total)
def plusFour():
    global total
    total += 4
    print(total)
def plusFive():
    global total
    total += 5
    print(total)
def plusSix():
    global total
    total += 6
    print(total)
def plusSeven():
    global total
    total += 7
    print(total)
def plusEight():
    global total
    total += 8
    print(total)
def plusNine():
    global total
    total += 9
    print(total)
def plusZero():
    global total
    total += 0
    print(total)
def print1():
    print("temp")
def reset():
    global total
    total -= total
    print(total)
def donothing():
    fileroot = Toplevel(root)
    button = Button(fileroot, text="Do nothing button")
    button.pack()
def Help():
    time.sleep(0.5)
    webbrowser.open ("http://www.bbc.co.uk/bitesize/ks2/maths/number/using_calculator/read/1/")
def close():
    root.withdraw()




# Dropdown Menu

menu = Menu(root)
filemenu = Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=filemenu)
menu.add_cascade(label="Help", command=Help)
filemenu.add_command(label="Exit", command=close)


# Buttons for numbers


b1 = Button(root, width=15, text="1", font='courier', command = plusOne,)
b2 = Button(root, width=15, text="2", font='courier', command = plusTwo,)
b3 = Button(root, width=15, text="3", font='courier', command = plusThree,)
b4 = Button(root, width=15, text="4", font='courier', command = plusFour,)
b5 = Button(root, width=15, text="5", font='courier', command = plusFive,)
b6 = Button(root, width=15, text="6", font='courier', command = plusSix,)
b7 = Button(root, width=15, text="7", font='courier', command = plusSeven,)
b8 = Button(root, width=15, text="8", font='courier', command = plusEight,)
b9 = Button(root, width=15, text="9", font='courier', command = plusNine,)
b0 = Button(root, width=15, text="0", font='courier', command = plusZero,)
print1 = Button(root, width=15, text="=", font='courier', command = print1)
Reset = Button(root, width=15, text="reset", font='courier', command = reset)
b1.grid(row=0, column=0)
b2.grid(row=0, column=1)
b3.grid(row=1, column=0)
b4.grid(row=1, column=1)
b5.grid(row=2, column=0)
b6.grid(row=2, column=1)
b7.grid(row=3, column=0)
b8.grid(row=3, column=1)
b9.grid(row=4, column=0)
b0.grid(row=4, column=1)
print1.grid(row=5, column=1)
Reset.grid(row=5, column=0)


root.config(menu=menu)
root.mainloop()

1 个答案:

答案 0 :(得分:4)

You cant use pack and grid together. Use e.grid()

The crash error message should say that as well:

_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack