[英]tkinter “unknown option ”-padx“ ”
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.我正在使用tkinter
进行项目,我想更改按钮 styles 以使其看起来更好,但是当我开始导入tkinter.tkk
时,代码开始混乱。
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.我在互联网上找不到任何明确的解决方案,而且当我使用tkinter
和tkinter.tkk
导入另一个代码时,它工作正常。
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:\Users\lcc_zarkos\Desktop\UKIYO-dataframer\gui-tkinter.py", line 12, in <module>
button1 = Button(window,text='1',padx=40,pady=40)
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 612, in __init__
Widget.__init__(self, master, "ttk::button", kw)
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 557, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2569, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-padx"
Please let me know if you have any solutions.如果您有任何解决方案,请告诉我。
pad
the elements in the grid()
method.您必须在grid()
方法中pad
元素。This:这个:
button1 = Button(window, text='1', padx=40, pady=40)
...
button1.grid(row=1, column=1)
Must be done like this:必须这样做:
button1 = Button(window, text='1')
...
button1.grid(row=1, column=1, padx=40, pady=40)
Also you should avoid importing tkinter
and tkinter.ttk
at the same time using from... import *
: when you type Button(...)
, this calls the tkinter.ttk.Button
class instead of the tkinter.Button
class. Also you should avoid importing tkinter
and tkinter.ttk
at the same time using from... import *
: when you type Button(...)
, this calls the tkinter.ttk.Button
class instead of the tkinter.Button
class. And this class doesn't support the fg
and bg
arguments.而这个 class 不支持fg
和bg
arguments。 So prefer removing the line所以宁愿去掉这条线
from tkinter.ttk import *
This will avoid this error:这将避免此错误:
Traceback (most recent call last):
File "C:\Users\lcc_zarkos\Desktop\UKIYO-dataframer\gui-tkinter.py", line 25, in <module>
buttoneq = Button(window,text='=',fg='black',bg="silver")
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 612, in __init__
Widget.__init__(self, master, "ttk::button", kw)
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 557, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-fg"
If you still want to import tkinter.ttk
, import it, for example, like that:如果您仍想导入tkinter.ttk
,请导入它,例如,像这样:
import tkinter.ttk as ttk
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.您应该知道,Tkinter 条目小部件只能具有宽度属性,如果您想要 position 条目小部件然后使用 padx 和 pady 但是如果您想调整条目小部件的大小然后使用 ipadx 和 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.