我如何使用Tkinter菜单小部件?

时间:2018-12-22 16:12:01

标签: python user-interface tkinter

我不知道如何在代码中添加Tkinter菜单小部件,我在网站上尝试了该代码,但无法正常工作。它认为我只是愚蠢,但请帮助。仅出于记录目的,我查看了其他堆栈溢出解决方案,但它们没有起作用。

import pyautogui
import time
import tkinter as tk


class Coordinates():
    replayBtn = (100,350)
class YourGUI(tk.Tk):
    def __init__(self):
        # inherit tkinter's window methods
        tk.Tk.__init__(self)
#Enter X field and label ⬇
        tk.Label(self, text="ENTER X:").grid(row=0, column=3)
        self.inputX = tk.Entry(self)
        self.inputX.grid(row=0, column=1)
#Enter Y field and label ⬇
        tk.Label(self, text="ENTER Y:").grid(row=0, column=0)
        self.inputY = tk.Entry(self)
        self.inputY.grid(row=0, column=4)
        # Start Button ⬇
        tk.Button(self, text="start", command=self.do_conversion).grid(row=3, column=0, columnspan=2)
        # close button ⬇
        tk.Button(self, text="exit!", command=self.EXITME).grid(row=4, column=0, columnspan=2)


    def EXITME(self):
        exit(0)  # crashed prog so it closes
        # strtoint("crashmE!")

    def do_conversion(self):
        y = self.inputY.get()
        x = self.inputX.get()

        running = True
        try:
            x = int(x)
            y = int(y)
        except:
            print("Invalid point")
            exit(0)
            # strtoint("crashmE!")
        while running:
            pyautogui.click(x, y)

if __name__ == '__main__':
    your_gui = YourGUI()
    your_gui.title('Macro Clicker') # Set title
    your_gui.iconbitmap('favicon.ico') # Set icon
    your_gui.resizable(False, False)
    your_gui.mainloop()
time.sleep(0)

1 个答案:

答案 0 :(得分:0)

很抱歉,但是如果您不懒惰,您会发现有关tkinter菜单的大量示例。 甚至您的代码也与tkinter.menu无关!?

这是我的一个例子。 “信息”菜单为空,只是向您展示如何创建其他菜单。

from tkinter import Tk, Frame, Menu

class Gui(Frame):
    def __init__(self, master):
        self.master = master
        Frame.__init__(self, self.master) #main container
        self.grid()
        self.create_menu()

    def create_menu(self):
        self.menubar = Menu(self.master)
        self.theme = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="View", menu=self.theme)
        self.menubar.add_command(label="Info", command=self.passing)
        self.views = Menu(self.theme, tearoff=0)
        self.theme.add_cascade(label="Themes", menu=self.views)
        self.views.add_command(label="Default", command=self.passing)
        self.views.add_command(label="Red", command=self.passing)
        self.views.add_command(label="Blue", command=self.passing)
        self.views.add_command(label="Random", command=self.passing)
        self.master.configure(menu=self.menubar)

    def passing(self):
        print("Use your brain before asking questions on stackoverflow or use google if your brain is slow")

if __name__ == "__main__":
    root = Tk()
    root.geometry("300x200")
    my_gui = Gui(root)
    root.mainloop()