Найти - Пользователи
Полная версия: Вызов tkinter и ttk по отдельности (python 3)
Начало » GUI » Вызов tkinter и ttk по отдельности (python 3)
1
Ubhra
Импорт:
 from tkinter import *
from tkinter.ttk import*

Указываю явное использование виджета tkinter:
 fr_a1=tk.Frame(frame_1,bg='green')
fr_b1=tk.Frame(frame_1,bg='blue')
fr_a1.pack()
fr_b1.pack()

Ошибка:
 NameError: name 'tk' is not defined

Причем на ttk так же ругается.
rami
Ubhra
Импорт:
from tkinter import *
from tkinter.ttk import*
Так нельзя импортировать, в tkinter и tkinter.ttk есть виджеты с одинаковым именем, они будут драться до кровавых соплей за первое место.

Правильно будет так:
 from tkinter import Tk,Frame
from tkinter.ttk import Style,Label
root=Tk()
Style().theme_use('default')
Style().configure('a1.TLabel',background='yellow')
fr_a1=Frame(root,bg='green',width=111,height=111)
lb_a1=Label(root,style='a1.TLabel',text='Это Label')
fr_b1=Frame(root,bg='blue',width=111,height=111)
fr_a1.pack()
lb_a1.pack()
fr_b1.pack()
root.mainloop()

или так:
 from tkinter import Tk,Label
from tkinter.ttk import Style,Frame
root=Tk()
Style().theme_use('default')
Style().configure('a1.TFrame',background='green')
Style().configure('b1.TFrame',background='blue')
fr_a1=Frame(root,style='a1.TFrame',width=111,height=111)
lb_a1=Label(root,bg='yellow',text='Это Label')
fr_b1=Frame(root,style='b1.TFrame',width=111,height=111)
fr_a1.pack()
lb_a1.pack()
fr_b1.pack()
root.mainloop()

Ubhra
Спасибо!
А что делать, если мне нужен общий виджет в разных местах?
rami
Что такое “общий виджет” и “разные места”?
Ubhra
Использовать tk.Label() и ttk.Label() в одной программе.
rami
Тогда так, но смысла я в этом не вижу:
 import tkinter as tk
import tkinter.ttk as ttk
root=tk.Tk()
ttk.Style().theme_use('default')
ttk.Style().configure('a1.TLabel',background='yellow')
lb_tk=tk.Label(root,bg='coral',text='Это Label "tk"')
lb_ttk=ttk.Label(root,style='a1.TLabel',text='Это Label "ttk"')
lb_tk.pack()
lb_ttk.pack()
root.mainloop()
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB