小部件不会显示出来

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

Widgets wont Show Up

问题

我正在尝试制作一个非常简单的应用程序,所以概念是:我有一个主窗口,在窗口内部我想要有两个框架,分别是Options Frame和Content Frame,Options Frame将包含按钮,当点击按钮时,Content Frame中的内容将会改变。我的问题是,def create_options_frame 中的按钮不会显示出来,但我没有收到任何错误消息,请帮助我。

  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import messagebox
  4. class MainWindow(tk.Tk):
  5. def __init__(self):
  6. super().__init__()
  7. self.geometry('800x300')
  8. self.title('Catatanku')
  9. class OptionsView(ttk.Frame):
  10. def __init__(self, master, **kw):
  11. super().__init__(master, **kw)
  12. self.grid_rowconfigure(0, weight=1)
  13. self.grid_columnconfigure(1, weight=1)
  14. self.create_options_frame().grid(row=0, column=0, sticky='wns')
  15. self.create_content_frame().grid(row=0, column=1)
  16. def create_options_frame(self) -> ttk.Frame:
  17. self.options_frame = ttk.Frame(self)
  18. btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
  19. btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
  20. btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
  21. btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
  22. return self.options_frame
  23. def create_content_frame(self) -> ttk.Frame:
  24. self.content_frame = ttk.Frame(self)
  25. return self.content_frame
  26. if __name__=='__main__':
  27. root = tk.Tk()
  28. 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

  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import messagebox
  4. class MainWindow(tk.Tk):
  5. def __init__(self):
  6. super().__init__()
  7. self.geometry('800x300')
  8. self.title('Catatanku')
  9. class OptionsView(ttk.Frame):
  10. def __init__(self, master, **kw):
  11. super().__init__(master, **kw)
  12. self.grid_rowconfigure(0, weight=1)
  13. self.grid_columnconfigure(1, weight=1)
  14. self.create_options_frame().grid(row=0, column=0, sticky='wns')
  15. self.create_content_frame().grid(row=0, column=1)
  16. def create_options_frame(self) -> ttk.Frame:
  17. self.options_frame = ttk.Frame(self )
  18. btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
  19. btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
  20. btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
  21. btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
  22. return self.options_frame
  23. def create_content_frame(self) -> ttk.Frame:
  24. self.content_frame = ttk.Frame(self)
  25. return self.content_frame
  26. if __name__=='__main__':
  27. root = tk.Tk()
  28. root.mainloop()

答案1

得分: 2

我认为你需要做以下更改:

  • 创建MainWindow的实例,而不是tk.Tk的实例。
  • MainWindow内部创建OptionsView的实例。
  1. import tkinter as tk
  2. from tkinter import ttk
  3. class MainWindow(tk.Tk):
  4. def __init__(self):
  5. super().__init__()
  6. self.geometry('800x300')
  7. self.title('Catatanku')
  8. # 创建OptionsView的实例
  9. self.view = OptionsView(self)
  10. self.view.pack(fill="both", expand=1)
  11. class OptionsView(ttk.Frame):
  12. def __init__(self, master, **kw):
  13. super().__init__(master, **kw)
  14. self.grid_rowconfigure(0, weight=1)
  15. self.grid_columnconfigure(1, weight=1)
  16. self.create_options_frame().grid(row=0, column=0, sticky='wns')
  17. self.create_content_frame().grid(row=0, column=1)
  18. def create_options_frame(self) -> ttk.Frame:
  19. self.options_frame = ttk.Frame(self)
  20. btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
  21. btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
  22. btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
  23. btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
  24. return self.options_frame
  25. def create_content_frame(self) -> ttk.Frame:
  26. self.content_frame = ttk.Frame(self)
  27. return self.content_frame
  28. if __name__=='__main__':
  29. # 创建MainWindow的实例,而不是tk.Tk的实例
  30. root = MainWindow()
  31. root.mainloop()
英文:

I think what you need is:

  • create instance of MainWindow instead of tk.Tk
  • create instance of OptionsView inside MainWindow
  1. import tkinter as tk
  2. from tkinter import ttk
  3. class MainWindow(tk.Tk):
  4. def __init__(self):
  5. super().__init__()
  6. self.geometry('800x300')
  7. self.title('Catatanku')
  8. # create instance of OptionsView
  9. self.view = OptionsView(self)
  10. self.view.pack(fill="both", expand=1)
  11. class OptionsView(ttk.Frame):
  12. def __init__(self, master, **kw):
  13. super().__init__(master, **kw)
  14. self.grid_rowconfigure(0, weight=1)
  15. self.grid_columnconfigure(1, weight=1)
  16. self.create_options_frame().grid(row=0, column=0, sticky='wns')
  17. self.create_content_frame().grid(row=0, column=1)
  18. def create_options_frame(self) -> ttk.Frame:
  19. self.options_frame = ttk.Frame(self)
  20. btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
  21. btn_buat_catatan.pack(fill=tk.BOTH, expand=True)
  22. btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
  23. btn_buka_catatan.pack(fill=tk.BOTH, expand=True)
  24. return self.options_frame
  25. def create_content_frame(self) -> ttk.Frame:
  26. self.content_frame = ttk.Frame(self)
  27. return self.content_frame
  28. if __name__=='__main__':
  29. # create instance of MainWindow instead of tk.Tk
  30. root = MainWindow()
  31. root.mainloop()

huangapple
  • 本文由 发表于 2023年8月9日 15:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865381.html
匿名

发表评论

匿名网友

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

确定