英文:
Why python isn't using other Tkinter?
问题
我写了这段代码,但问题是Python只使用第一行来导入tkinter,而不使用我在之后使用的另外两行。我该如何解决这个问题?
我尝试了各种方法将tkinter导入为ttk,但根本不起作用。我只想知道如何将tkinter导入为ttk。
英文:
I wrote this code but the problem is the python is using only the first line to import tkinter and it doesn't use the other two lines that I used after that.
How can I solve this?
I tried various ways to import tkinter as ttk but it doesn't work at all. I just want a way to import tkinter as ttk.enter image description here
答案1
得分: 1
我不确定具体的问题。我没有看到您使用消息框或任何ttk小部件。但如果您这样做,应该可以正常工作。与下面的示例进行比较:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
info = ttk.Label(root, text='文本')
info.pack(padx=50, pady=20)
root.mainloop()
这对您有用吗?
英文:
Hmmm..., not sure about the exact problem. I don't see you using either messagebox or any ttk widgets. But it should work fine if you do. Compare with the example below:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
info = ttk.Label(root, text='Text')
info.pack(padx=50, pady=20)
root.mainloop()
Does that work for you?
答案2
得分: 0
-
为什么Python没有使用其他的Tkinter?
- 你同时使用了
msg = tk.Button
和msg = tk.Label
,这会引起冲突。 - 为
Toplever
小部件添加extra_window
。 - 删除
extra_window.mainloop()
。一个mainloop()
足以运行整个脚本。
- 你同时使用了
修改后的代码段:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
def create_window():
extra_window = tk.Toplevel()
msg_label = tk.Label(extra_window, text='你想吃什么',
bg='黑色', fg='白色', width=20, height=2)
entry = tk.Entry(extra_window, bg='白色', fg='黑色')
msg_label.pack()
entry.pack(expand=True)
window = tk.Tk()
window.geometry('300x400')
window.title('游戏')
msg = tk.Button(window, text='开始', bg='紫色', fg='白色', command=create_window)
msg.pack()
window.mainloop()
屏幕截图:
英文:
> Why python isn't using other Tkinter?
- You are using both duplicates one for
msg = tk.Button
andmsg =
. That will cause conflict.
tk.Label - Add
extra_window
forToplever
widgets. - remove extra_window.mainloop(). One mainloop() will do entire script.
Snippet modified:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
def create_window():
extra_window = tk.Toplevel()
msg_label = tk.Label(extra_window, text='What you wanna eat',
bg='black', fg='white', width=20, height=2)
entry = tk.Entry(extra_window, bg='white', fg='black')
msg_label.pack()
entry.pack(expand=True)
window = tk.Tk()
window.geometry('300x400')
window.title('Game')
msg = tk.Button(window, text='Start', bg='purple', fg='white', command=create_window)
msg.pack()
window.mainloop()
Screenshot:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论