如何制作可选的 tkinter GUI?

发布于 2025-01-13 03:33:07 字数 334 浏览 0 评论 0原文

我有一个 tkinter GUI 代码,它执行映射到不同按钮/小部件的不同功能,我想为用户提供选择打开 GUI 窗口或直接从命令行执行的选项。

我尝试将 mainloop() 保留在可选开关内,例如:

if gui == "1":
    root.mainloop()
else:
    #command mode
    # call required functions here using user switches.

这是执行此操作的正确方法吗?当我在linux系统中尝试时,这是有效的。

I have a tkinter GUI code that executes different functions which are mapped to different buttons/widgets, I want to give users the option of choosing to open the GUI window or directly execute from the command line.

I tried keeping mainloop() inside an optional switch like:

if gui == "1":
    root.mainloop()
else:
    #command mode
    # call required functions here using user switches.

Is this the correct way of doing this? this is working when i tried in linux system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢望 2025-01-20 03:33:07
root = Tk()
root.title('ATM v.keypad buttons //////dont acc work///////')
            
        

# creates the buttons adds no value tho

# size of button
gridlabel1 = Button(root, text='1', padx=50, pady=50)
gridlabel2 = Button(root, text='2', padx=50, pady=50)
gridlabel3 = Button(root, text='3', padx=50, pady=50)
gridlabel4 = Button(root, text='4', padx=50, pady=50)
gridlabel5 = Button(root, text='5', padx=50, pady=50)
gridlabel6 = Button(root, text='6', padx=50, pady=50)
gridlabel7 = Button(root, text='7', padx=50, pady=50)
gridlabel8 = Button(root, text='8', padx=50, pady=50)
gridlabel9 = Button(root, text='9', padx=50, pady=50)
gridlabel0 = Button(root, text='0', padx=50, pady=50)
cancelbutton = Button(root, text='cancel', padx=75, pady=50)
clearbutton = Button(root, text='clear', padx=75, pady=50)
enterbutton = Button(root, text='enter', padx=75, pady=50)
# prints the button at set location
gridlabel1.grid(row=0, column=0)
gridlabel2.grid(row=0, column=1)
gridlabel3.grid(row=0, column=2)
gridlabel4.grid(row=1, column=0)
gridlabel5.grid(row=1, column=1)
gridlabel6.grid(row=1, column=2)
gridlabel7.grid(row=2, column=0)
gridlabel8.grid(row=2, column=1)
gridlabel9.grid(row=2, column=2)
gridlabel0.grid(row=3, column=1)
cancelbutton.grid(row=0, column=4)
clearbutton.grid(row=1, column=4)
enterbutton.grid(row=2, column=4)
root.mainloop()

这是我的示例,请注意我如何将 root.mainloop() 放在最后
缩进,因为它在 if 语句中

root = Tk()
root.title('ATM v.keypad buttons //////dont acc work///////')
            
        

# creates the buttons adds no value tho

# size of button
gridlabel1 = Button(root, text='1', padx=50, pady=50)
gridlabel2 = Button(root, text='2', padx=50, pady=50)
gridlabel3 = Button(root, text='3', padx=50, pady=50)
gridlabel4 = Button(root, text='4', padx=50, pady=50)
gridlabel5 = Button(root, text='5', padx=50, pady=50)
gridlabel6 = Button(root, text='6', padx=50, pady=50)
gridlabel7 = Button(root, text='7', padx=50, pady=50)
gridlabel8 = Button(root, text='8', padx=50, pady=50)
gridlabel9 = Button(root, text='9', padx=50, pady=50)
gridlabel0 = Button(root, text='0', padx=50, pady=50)
cancelbutton = Button(root, text='cancel', padx=75, pady=50)
clearbutton = Button(root, text='clear', padx=75, pady=50)
enterbutton = Button(root, text='enter', padx=75, pady=50)
# prints the button at set location
gridlabel1.grid(row=0, column=0)
gridlabel2.grid(row=0, column=1)
gridlabel3.grid(row=0, column=2)
gridlabel4.grid(row=1, column=0)
gridlabel5.grid(row=1, column=1)
gridlabel6.grid(row=1, column=2)
gridlabel7.grid(row=2, column=0)
gridlabel8.grid(row=2, column=1)
gridlabel9.grid(row=2, column=2)
gridlabel0.grid(row=3, column=1)
cancelbutton.grid(row=0, column=4)
clearbutton.grid(row=1, column=4)
enterbutton.grid(row=2, column=4)
root.mainloop()

that was my example notice how I put root.mainloop() at the end
was indented because it was in a if statement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文