在Python 3.3中的GUI?

2024-04-27 03:42:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为我的程序制作一个图形用户界面形式的菜单,用户可以通过点击一个框来选择他们想要的,而不是输入数字/字母。到目前为止我得到的是:

while True:
    encrypt = False
    decrypt = False
    viewz = False
    errorz = False
    inp = input("Do you want to [E]ncode, [D]ecode, [V]iew Code or [EX]it:")
    inp = inp.lower()
    if (inp != "e"  and inp != "d" and inp != "encode" and inp != "decode" and inp != "v" and inp != "view code" ):
        print("Input error")
        print()
        continue
    if inp == "e" or inp == "encode":
        encrypt = True
    if inp == "d" or inp == "decode":
        decrypt = True

Tags: orand程序falsetrueif菜单图形用户界面
1条回答
网友
1楼 · 发布于 2024-04-27 03:42:36

下面是一个在windows上运行的Python3.3的tkinter示例。不完全确定你的目的是什么,但希望能有所帮助。在

import tkinter as tk

window = tk.Tk()

encrypt = False
decrypt = False
viewz = False
errorz = False # Not sure why you had this, but included it as it may be handy for you

def encode():
    encrypt = True
    window.destroy()

def decode():
    decrypt =True
    window.destroy()

def viewcode():
    viewz = True # I think this is what you were going to do here?
    window.destroy()

def Exit():
    window.destroy() # Assuming you want the exit button to exit the tk window

label = tk.Label(text = "Do you want to:")
encodebutton = tk.Button(text = "Encode", command = encode)
decodebutton = tk.Button(text = "Decode", command = decode)
viewcodebutton = tk.Button(text = "View Code", command = viewcode)
exitbutton = tk.Button(text = "Exit", command = Exit)
# Theres heaps more you can do with tkinter, google it and give it a go it's heaps of   fun.
label.pack()
encodebutton.pack()
decodebutton.pack()
viewcodebutton.pack()
exitbutton.pack()

window.mainloop()
# After this you go on with your code :p

相关问题 更多 >