首页 > 解决方案 > python结构约定tkinter

问题描述

我不确定如何构建我的 python 代码以遵循编程约定,我的老师告诉我从一开始就启动测验 GUI 对象而不是主,但我不确定她的意思。这是我的代码的基本版本:

# Import everything from Tkinter module
from tkinter import *


def function():
    blajslaksjd


def another_function():
    blahblah


# quiz is the tkinter box
# I am going to change the positions to the help menu bar
quiz = Tk()
quiz.geometry('800x800-100+20')

# Menu bar
menu_bar = Menu(quiz)
quiz.config(menu=menu_bar)

help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)

# Global variables 
variable = 0
# Another variable
question = 0
# Displays the actual question
question_label = Label(text="insert question", font="bold, 13")

# Radio buttons for some functons
option_1 = Radiobutton(quiz, text="text", value=1)
option_2 = Radiobutton(quiz, text="text", value=2)
option_3 = Radiobutton(quiz, text="text", value=3)
option_4 = Radiobutton(quiz, text="text", value=4)

# Label for function
blah = Label(text="Blah")

# Button for function  
blahh = Button(quiz, text="BLAHH")

# Display tkinter box
quiz.mainloop()

如果我把它改成这样会更好吗?我将帮助定义 tkinter 框的属性的代码移到顶部。

# Import everything from Tkinter module
from tkinter import *


# quiz is the tkinter box
quiz = Tk()
quiz.geometry('800x800-100+20')

# Menu bar
menu_bar = Menu(quiz)
quiz.config(menu=menu_bar)

help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)


def function():
    blajslaksjd


def another_function():
    blahblah


# Global variables 
variable = 0
# Another variable
question = 0
# Displays the actual question
question_label = Label(text="insert question", font="bold, 13")

# Radio buttons for some functons
option_1 = Radiobutton(quiz, text="text", value=1)
option_2 = Radiobutton(quiz, text="text", value=2)
option_3 = Radiobutton(quiz, text="text", value=3)
option_4 = Radiobutton(quiz, text="text", value=4)

# Label for function
blah = Label(text="Blah")

# Button for function  
blahh = Button(quiz, text="BLAHH")

# Display tkinter box
quiz.mainloop()

或这个?我将帮助定义 tkinter 框属性的代码移到了底部。

# Import everything from Tkinter module
from tkinter import *


def function():
    blajslaksjd


def another_function():
    blahblah


# Global variables 
variable = 0
# Another variable
question = 0
# Displays the actual question
question_label = Label(text="insert question", font="bold, 13")

# Radio buttons for some functons
option_1 = Radiobutton(quiz, text="text", value=1)
option_2 = Radiobutton(quiz, text="text", value=2)
option_3 = Radiobutton(quiz, text="text", value=3)
option_4 = Radiobutton(quiz, text="text", value=4)

# Label for function
blah = Label(text="Blah")

# Button for function  
blahh = Button(quiz, text="BLAHH")

# quiz is the tkinter box
quiz = Tk()
quiz.geometry('800x800-100+20')

# Menu bar
menu_bar = Menu(quiz)
quiz.config(menu=menu_bar)

help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)

# Display tkinter box
quiz.mainloop()

标签: pythonpython-3.xtkinter

解决方案


推荐阅读