I want to make a GUI but i get this error: self.frame.grid(row=0, column=0, sticky="nsew") AttributeError: 'function' object has no attribute 'grid'

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

I want to make a GUI but i get this error: self.frame.grid(row=0, column=0, sticky="nsew") AttributeError: 'function' object has no attribute 'grid'

问题

抱歉,以下是您的代码的翻译部分:

  1. 抱歉关于这个问题的布局有些混乱这是我第一次在stackoverflow上发帖我想要为井字棋创建一个GUI游戏菜单)。并且希望能够在GUI中的任何位置放置按钮因此我使用了Grid布局
  2. ```python
  3. import tkinter as tk
  4. LARGE_FONT = ("Verdana", 12)
  5. class SeaofBTCapp(tk.Tk):
  6. def __init__(self, *args, **kwargs):
  7. tk.Tk.__init__(self, *args, **kwargs)
  8. container = tk.Frame(self)
  9. container.pack(side="top", fill="both", expand=True)
  10. container.grid_rowconfigure(0, weight=1)
  11. container.grid_columnconfigure(0, weight=1)
  12. self.frames = {}
  13. for F in (MainWindow, Game, Difficulty):
  14. frame = F(container, self)
  15. self.frames[F] = frame
  16. frame.grid(row=0, column=0, sticky="nsew")
  17. self.show_frame(StartPage)
  18. def show_frame(self, cont):
  19. frame = self.frames[cont]
  20. frame.tkraise()
  21. class MainWindow(tk.Tk):
  22. def __init__(self, parent, controller):
  23. tk.Frame.__init__(self, parent)
  24. label = tk.Label(self, text="欢迎来到井字棋", font=LARGE_FONT)
  25. label.pack(pady=10, padx=10)
  26. button1 = tk.Button(self, text="开始游戏",
  27. command=lambda: controller.show_frame(Game))
  28. button1.pack()
  29. button2 = tk.Button(self, text="难度选择",
  30. command=lambda: controller.show_frame(Difficulty))
  31. button2.pack()
  32. button3 = tk.Button(self, text="退出", command=self.Quit)
  33. button3.pack()
  34. label1 = tk.Label(self, text="由VindictaOG制作")
  35. label1.pack()
  36. def Quit(self):
  37. exit()
  38. class Game(tk.Tk):
  39. def __init__(self, parent, controller):
  40. tk.Frame.__init__(self, parent)
  41. button1 = tk.Button(self, text="新游戏")
  42. button1.pack()
  43. button2 = tk.Button(self, text="返回主菜单",
  44. command=lambda: controller.show_frame(MainWindow))
  45. button2.pack()
  46. class Difficulty(tk.Tk):
  47. def __init__(self, parent, controller):
  48. tk.Frame.__init__(self, parent)
  49. button1 = tk.Button(self, text="1对1", command=lambda: controller.show_frame(MainWindow))
  50. button1.pack()
  51. button2 = tk.Button(self, text="返回主菜单",
  52. command=lambda: controller.show_frame(Game))
  53. button2.pack()
  54. gui = SeaofBTCapp()
  55. gui.mainloop()

但是当我使用Grid布局时,我收到以下错误:

  1. Traceback (most recent call last):
  2. File "/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py", line 82, in <module>
  3. gui = SeaofBTCapp()
  4. File "/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py", line 27, in __init__
  5. frame.grid(row=0, column=0, sticky="nsew")
  6. TypeError: wm_grid() got an unexpected keyword argument 'row'

我尝试使用pack布局,但那不起作用,有人知道如何修复这个问题吗?

英文:

Sorry about the layout of this question, my first time working with stackoverflow posts.
I want to make a GUI for Tic Tac Toe (Game menu). And have the abillity to put buttons where ever i want in the GUI so i used Grid.

  1. import tkinter as tk
  2. LARGE_FONT= (&quot;Verdana&quot;, 12)
  3. class SeaofBTCapp(tk.Tk):
  4. def __init__(self, *args, **kwargs):
  5. tk.Tk.__init__(self, *args, **kwargs)
  6. container = tk.Frame(self)
  7. container.pack(side=&quot;top&quot;, fill=&quot;both&quot;, expand=True)
  8. container.grid_rowconfigure(0, weight=1)
  9. container.grid_columnconfigure(0, weight=1)
  10. self.frames = {}
  11. for F in (MainWindow, Game, Difficulty):
  12. frame = F(container, self)
  13. self.frames[F] = frame
  14. frame.grid(row=0, column=0, sticky=&quot;nsew&quot;)
  15. self.show_frame(StartPage)
  16. def show_frame(self, cont):
  17. frame = self.frames[cont]
  18. frame.tkraise()
  19. class MainWindow(tk.Tk):
  20. def __init__(self, parent, controller):
  21. tk.Frame.__init__(self, parent)
  22. label = tk.Label(self, text=&quot;Welcom to TIC TAC TOE&quot;, font=LARGE_FONT)
  23. label.pack(pady=10, padx=10)
  24. button1 = tk.Button(self, text=&quot;Start&quot;,
  25. command=lambda: controller.show_frame(Game))
  26. button1.pack()
  27. button2 = tk.Button(self, text=&quot;Diffeculty&quot;,
  28. command=lambda: controller.show_frame(Difficulty))
  29. button2.pack()
  30. button3 = tk.Button(self, text=&quot;Quit&quot;, command=self.Quit)
  31. button3.pack()
  32. label1 = tk.Label(self, text=&quot;Made by VindictaOG&quot;)
  33. label1.pack()
  34. def Quit(self):
  35. exit()
  36. class Game(tk.Tk):
  37. def __init__(self, parent, controller):
  38. tk.Frame.__init__(self, parent)
  39. button1 = tk.Button(self, text=&quot;New Game&quot;)
  40. button1.pack()
  41. button2= tk.Button(self, text=&quot;Back to homescreen&quot;,
  42. command=lambda: controller.show_frame(MainWindow))
  43. button2.pack()
  44. class Difficulty(tk.Tk):
  45. def __init__(self, parent, controller):
  46. tk.Frame.__init__(self, parent)
  47. button1 = tk.Button(self, text=&quot;1V1&quot;, command=lambda: controller.show_frame(MainWindow))
  48. button1.pack()
  49. button2 = tk.Button(self, text=&quot;Back to homescreen&quot;,
  50. command=lambda: controller.show_frame(Game))
  51. button2.pack()
  52. gui = SeaofBTCapp()
  53. gui.mainloop()

But when i use grid i get this error:

  1. Traceback (most recent call last):
  2. File &quot;/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py&quot;, line 82, in &lt;module&gt;
  3. gui = SeaofBTCapp()
  4. File &quot;/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py&quot;, line 27, in __init__
  5. frame.grid(row=0, column=0, sticky=&quot;nsew&quot;)
  6. TypeError: wm_grid() got an unexpected keyword argument &#39;row&#39;

I tried it with pack but that would not work, does someone know how to fix this?

答案1

得分: 0

应该从tk.Frame继承,而不是从tk.Tk继承MainWindow、Game和Difficulty。另外,StartPage未定义。@acw1668

使用(tk.Frame, tk.Tk)而不仅仅是(tk.Tk)解决了问题。

英文:

Should inherit from tk.Frame instead of tk.Tk for MainWindow, Game and Difficulty. Also StartPage is not defined. @acw1668

Using (tk.Frame, tk.Tk) instead of just (tk.Tk) solved the problem.

huangapple
  • 本文由 发表于 2020年1月7日 00:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615257.html
匿名

发表评论

匿名网友

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

确定