I am working on a project using tkinter
and I wanted to change button styles to make it look better, but when I start import tkinter.tkk
the code starts messing up.
I could not find any clear solution on the Internet and also when I start another code with both tkinter
and tkinter.tkk
imported it works fine.
Here is the working code:
from tkinter import * from tkinter.ttk import * root = Tk() root.geometry('100x100') style = Style() style.configure('TButton', font = ('calibri', 10, 'bold', 'underline'), foreground = 'red') btn1 = Button(root, text = 'Quit !', style = 'TButton', command = root.destroy) btn1.grid(row = 0, column = 3, padx = 100) btn2 = Button(root, text = 'Click me !', command = None) btn2.grid(row = 1, column = 3, pady = 10, padx = 100) root.mainloop()
And here is the code that has a problem:
from tkinter import * from tkinter.ttk import * window= Tk() window.title('customers information') e = Entry(window, width=50) button1 = Button(window,text='1',padx=40,pady=40) button2 = Button(window,text='2',padx=40,pady=40) button3 = Button(window,text='3',padx=40,pady=40) button4 = Button(window,text='4',padx=40,pady=40) button5 = Button(window,text='5',padx=40,pady=40) button6 = Button(window,text='6',padx=40,pady=40) button7 = Button(window,text='7',padx=40,pady=40) button8 = Button(window,text='8',padx=40,pady=40) button9 = Button(window,text='9',padx=40,pady=40) button0 = Button(window,text='0',padx=40,pady=40) buttoneq = Button(window,text='=',padx=40,pady=40,fg='black',bg="silver") buttonplus = Button(window,text='+',padx=40,pady=40,fg='black',bg='silver') buttonminus = Button(window,text='-',padx=40,pady=40,fg='black',bg='silver') buttonclear = Button(window,text='C',padx=40,pady=40,fg='black',bg='silver') button1.grid(row=1,column=1) button2.grid(row=1,column=2) button3.grid(row=1,column=3) button4.grid(row=2,column=1) button5.grid(row=2,column=2) button6.grid(row=2,column=3) button7.grid(row=3,column=1) button8.grid(row=3,column=2) button9.grid(row=3,column=3) button0.grid(row=4,column=1) buttonplus.grid(row=4,column=2) buttonminus.grid(row=4,column=3) buttoneq.grid(row=5,column=2) buttonclear.grid(row=5,column=1) e.grid(row=0,column=0,columnspan=5,padx=10,pady=10) window.mainloop()
And the exception :
Traceback (most recent call last): File "c:Userslcc_zarkosDesktopUKIYO-dataframergui-tkinter.py", line 12, in <module> button1 = Button(window,text='1',padx=40,pady=40) File "C:Userslcc_zarkosAppDataLocalProgramsPythonPython39libtkinterttk.py", line 612, in __init__ Widget.__init__(self, master, "ttk::button", kw) File "C:Userslcc_zarkosAppDataLocalProgramsPythonPython39libtkinterttk.py", line 557, in __init__ tkinter.Widget.__init__(self, master, widgetname, kw=kw) File "C:Userslcc_zarkosAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 2569, in __init__ self.tk.call( _tkinter.TclError: unknown option "-padx"
Please let me know if you have any solutions.
Advertisement
Answer
You should know, Tkinter Entry widget can have width property only AS if you want to position the entry widget then use padx and pady however if you want to resize the entry widget then use ipadx and ipady.
from tkinter import * #Create an instance of tkinter window root= Tk() root.geometry("600x450") e= Entry(root, width= 20) e.pack(ipadx=30, ipady= 30) root.mainloop()