英文:
Widgets wont Show Up
问题
我正在尝试创建一个非常简单的应用程序,所以概念是:我有一个主窗口,在窗口内部,我想要有2个框架,分别是"Options Frame"和"Content Frame",所以"Options Frame"将包含按钮,当按钮被点击时,"Content Frame"中的内容将会改变。我的问题是,"create_options_frame"中的按钮不会显示出来,但我没有收到任何错误消息,请帮助我。
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('800x300')
self.title('Catatanku')
class OptionsView(ttk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
self.create_options_frame().grid(row=0, column=0, sticky='wns')
self.create_content_frame().grid(row=0, column=1)
def create_options_frame(self) -> ttk.Frame:
self.options_frame = ttk.Frame(self )
btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
return self.options_frame
def create_content_frame(self) -> ttk.Frame:
self.content_frame = ttk.Frame(self)
return self.content_frame
if __name__=='__main__':
root = tk.Tk()
root.mainloop()
英文:
im trying to make a very simple app, so the concept is: i have a main window, and inside the window i want have 2 frames which are Options Frame and Content Frame, so the Options Frame will contain Buttons, when a button is clicked, the content in Content Frame will change. My problem is, buttons on def create_options_frame wont show up, but i don't get any error message, please help ,me
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('800x300')
self.title('Catatanku')
class OptionsView(ttk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
self.create_options_frame().grid(row=0, column=0, sticky='wns')
self.create_content_frame().grid(row=0, column=1)
def create_options_frame(self) -> ttk.Frame:
self.options_frame = ttk.Frame(self )
btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
return self.options_frame
def create_content_frame(self) -> ttk.Frame:
self.content_frame = ttk.Frame(self)
return self.content_frame
if __name__=='__main__':
root = tk.Tk()
root.mainloop()
答案1
得分: 2
我认为你需要的是:
- 创建
MainWindow
的实例,而不是tk.Tk
的实例。 - 在
MainWindow
内创建OptionsView
的实例。
import tkinter as tk
from tkinter import ttk
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('800x300')
self.title('Catatanku')
# 创建OptionsView的实例
self.view = OptionsView(self)
self.view.pack(fill="both", expand=1)
class OptionsView(ttk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
self.create_options_frame().grid(row=0, column=0, sticky='wns')
self.create_content_frame().grid(row=0, column=1)
def create_options_frame(self) -> ttk.Frame:
self.options_frame = ttk.Frame(self)
btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
return self.options_frame
def create_content_frame(self) -> ttk.Frame:
self.content_frame = ttk.Frame(self)
return self.content_frame
if __name__=='__main__':
# 创建MainWindow的实例,而不是tk.Tk的实例
root = MainWindow()
root.mainloop()
英文:
I think what you need is:
- create instance of
MainWindow
instead oftk.Tk
- create instance of
OptionsView
insideMainWindow
import tkinter as tk
from tkinter import ttk
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('800x300')
self.title('Catatanku')
# create instance of OptionsView
self.view = OptionsView(self)
self.view.pack(fill="both", expand=1)
class OptionsView(ttk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
self.create_options_frame().grid(row=0, column=0, sticky='wns')
self.create_content_frame().grid(row=0, column=1)
def create_options_frame(self) -> ttk.Frame:
self.options_frame = ttk.Frame(self)
btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
return self.options_frame
def create_content_frame(self) -> ttk.Frame:
self.content_frame = ttk.Frame(self)
return self.content_frame
if __name__=='__main__':
# create instance of MainWindow instead of tk.Tk
root = MainWindow()
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论