import guizero as gz
import category_gui
def exit_dvd_app():
exit()
pass
class dvd_rental_gui:
app = None
main_page = None
category_gui = None
app_window_width = 500
app_window_height = 500
app_window_title = 'DVD Rental App'
def __init__(self):
self.app = gz.App(title=self.app_window_title)
pass
def build_main_page_gui(self):
self.main_page = box = gz.Box(self.app)
gz.Box(box, height=50)
title_text = gz.Text(box, 'You can perform the CRUD operations on the tables of DVD Rental Application!!!')
gz.Box(box, height=50)
read_button = gz.PushButton(box, text='Category Table!', command=self.build_category_crud_gui)
gz.Box(box, height=50)
exit_button = gz.PushButton(box, text='Exit', command=exit_dvd_app)
self.app.display()
pass
def build_category_crud_gui(self):
self.main_page.hide()
self.category_gui = category_gui.categoru_gui(self.app, self.main_page)
self.category_gui.build_gui()
pass
pass
以上为代码,不加入以下几句界面就不会显示,但是课件里是没有这两句的所以我想问问这是为啥
if __name__ == '__main__':
dvd_gui = dvd_rental_gui()
dvd_gui.build_main_page_gui()
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
这两句代码是用于判断当前脚本是否被当做主程序运行,如果是,则执行 dvd_rental_gui
类的实例化和 build_main_page_gui
方法的调用,从而启动 GUI 界面。如果不加入这两句代码,则这个脚本只能作为模块被导入,但不会启动 GUI 界面。
在 PyCharm 中,如果直接运行该脚本,则会默认将其作为主程序运行,因此需要加入这两句代码,否则界面不会显示。但如果你在课件中看到的示例代码没有这两句话,可能是因为示例代码是在交互式环境下运行的,并不需要判断是否为主程序。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
没有包含if __name__ == '__main__':
和dvd_gui.build_main_page_gui()
这两行代码时,界面不会显示,是因为这两行代码的作用是创建dvd_rental_gui
类的实例并调用build_main_page_gui()
方法来构建和显示界面。
当一个Python脚本作为主程序执行时,特殊变量__name__
的值被设置为'__main__'
。而当一个Python模块被其他脚本导入时,__name__
的值被设置为模块的名称。通过包含if __name__ == '__main__':
条件,你确保其中的代码只在直接运行脚本时执行,而不是在作为模块导入时执行。
在你的代码中,dvd_rental_gui
类和它的方法被定义,但是没有包含if __name__ == '__main__':
块,因此没有执行创建类实例和显示界面的代码。这就是为什么在运行脚本时没有看到界面。
通过添加if __name__ == '__main__':
块以及后续的dvd_gui = dvd_rental_gui()
和dvd_gui.build_main_page_gui()
代码,你确保只有在直接运行脚本时才构建和显示界面。
包含这个块可以让你将脚本作为独立的程序运行,但是如果你将模块导入到其他脚本中,与界面相关的代码不会自动执行。这样你就可以在其他脚本中灵活使用dvd_rental_gui
类和它的方法,而不会启动界面。
将 exit_dvd_app
函数中的 pass
删除,以避免出现无意义代码。
将 category_gui
类名修改为 category_gui_new
等其他名字,以避免可能的名称冲突。
在 dvd_rental_gui
类的 __init__
方法中,将 self.app.display()
方法移动到最后一行,以确保应用程序窗口已显示。
在 PyCharm 工具栏的 "Run" 菜单下,选择 "Edit Configurations..." 并取消勾选 "Emulate terminal in output console" 选项,同时在 "Environment" 标签页下添加或修改 "PYTHONUNBUFFERED" 环境变量的值为 "1"。
下面是修改后的代码,供您参考:
import guizero as gz
import category_gui_new
def exit_dvd_app():
exit()
class dvd_rental_gui:
app = None
main_page = None
category_gui = None
app_window_width = 500
app_window_height = 500
app_window_title = 'DVD Rental App'
def __init__(self):
self.app = gz.App(title=self.app_window_title)
self.app.display()
def build_main_page_gui(self):
self.main_page = box = gz.Box(self.app)
gz.Box(box, height=50)
title_text = gz.Text(box, 'You can perform the CRUD operations on the tables of DVD Rental Application!!!')
gz.Box(box, height=50)
read_button = gz.PushButton(box, text='Category Table!', command=self.build_category_crud_gui)
gz.Box(box, height=50)
exit_button = gz.PushButton(box, text='Exit', command=exit_dvd_app)
def build_category_crud_gui(self):
self.main_page.hide()
self.category_gui = category_gui_new.category_gui_new(self.app, self.main_page)
self.category_gui.build_gui()
不知道你这个问题是否已经解决, 如果还没有解决的话:声明:本人使用的版本为pycharm2020专业版,版本不一样对应的某些窗口会有细微差异。
昨天手贱,把pycharm窗口小化,菜单栏各种东西都挤到一块去了,鼠标一抖,窗口直接变成了白板代码,菜单栏各种栏啥都没了。
界面就是这样:
对,你没看错,除了代码,怎么都不显示,甚至文件名都没了。
解决办法:
shift双击调出搜索,输入view,点击more,如图所示:
(如果你啥都没了,和我第一张图一样,可以和我一样进行设置,最后的界面会在最后展示)
点击more之后,看到这些开关,看到有个Main Menu的,对,主菜单就是他,打开它!
wow!回来了!
关于行号和左侧菜单显示,直接在左侧右键,看到弹窗,打勾,这几个英文我就不解释了。
还有图示中的菜单栏下面的工具栏,就在这里调出来
view – appearance – 打勾
最后你们发现一个细节没,我每次点击文件,他都打开当前一个文件,并不会多个文件并排显示,这令我换文件写代码很不爽。
找到window – editor Tabs – configure editor Tabs
点击之后出现这个页面,在None的那个位置下拉选择Top
注明:窗口各类的可以在本页面的appearance中设置,编辑小窗口的设置在editor Tabs中设置。
选择好Top之后点击Apply,再点击OK
最后你的窗口就恢复啦!