为什么Python没有使用其他的Tkinter?

huangapple go评论112阅读模式
英文:

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小部件。但如果您这样做,应该可以正常工作。与下面的示例进行比较:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. root = tk.Tk()
  4. info = ttk.Label(root, text='文本')
  5. info.pack(padx=50, pady=20)
  6. 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:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. root = tk.Tk()
  4. info = ttk.Label(root, text='Text')
  5. info.pack(padx=50, pady=20)
  6. root.mainloop()

Does that work for you?

答案2

得分: 0

  • 为什么Python没有使用其他的Tkinter?

    • 你同时使用了msg = tk.Buttonmsg = tk.Label,这会引起冲突。
    • Toplever小部件添加extra_window
    • 删除extra_window.mainloop()。一个mainloop()足以运行整个脚本。

修改后的代码段:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import messagebox
  4. def create_window():
  5. extra_window = tk.Toplevel()
  6. msg_label = tk.Label(extra_window, text='你想吃什么',
  7. bg='黑色', fg='白色', width=20, height=2)
  8. entry = tk.Entry(extra_window, bg='白色', fg='黑色')
  9. msg_label.pack()
  10. entry.pack(expand=True)
  11. window = tk.Tk()
  12. window.geometry('300x400')
  13. window.title('游戏')
  14. msg = tk.Button(window, text='开始', bg='紫色', fg='白色', command=create_window)
  15. msg.pack()
  16. window.mainloop()

屏幕截图:

为什么Python没有使用其他的Tkinter?

为什么Python没有使用其他的Tkinter?

英文:

> Why python isn't using other Tkinter?

  • You are using both duplicates one for msg = tk.Button and msg =
    tk.Label
    . That will cause conflict.
  • Add extra_window for Toplever widgets.
  • remove extra_window.mainloop(). One mainloop() will do entire script.

Snippet modified:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import messagebox
  4. def create_window():
  5. extra_window = tk.Toplevel()
  6. msg_label = tk.Label(extra_window, text='What you wanna eat',
  7. bg='black', fg='white', width=20, height=2)
  8. entry = tk.Entry(extra_window, bg='white', fg='black')
  9. msg_label.pack()
  10. entry.pack(expand=True)
  11. window = tk.Tk()
  12. window.geometry('300x400')
  13. window.title('Game')
  14. msg = tk.Button(window, text='Start', bg='purple', fg='white', command=create_window)
  15. msg.pack()
  16. window.mainloop()

Screenshot:

为什么Python没有使用其他的Tkinter?

为什么Python没有使用其他的Tkinter?

huangapple
  • 本文由 发表于 2023年6月26日 02:11:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定