-2

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
    Hi and welcome to Stack Overflow. Please note that Stack Overflow has a policy of not permitting images of code. For the reasons why, please see [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – Luke Woodward Jun 25 '23 at 18:33
  • You are importing `ttk` just fine. I'm not sure what you expect. Nothing in your code is actually using `ttk`. (ex: `ttk.Label(...)`). It's like you threw a bunch of clothes in your luggage, but you're confused as to why you aren't wearing them. You have to actually put them on. – OneMadGypsy Jun 25 '23 at 23:19

2 Answers2

1

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?

figbeam
  • 7,001
  • 2
  • 12
  • 18
0

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:

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:

enter image description here

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19