使用tkinter使用输入小部件制作新窗口

时间:2018-08-04 14:31:44

标签: python tkinter

我似乎无法在新窗口中显示输入代码

它仅显示一个没有标签和输入小部件的新空白窗口

我想为学校项目制作一个简单的GUI,但是我不想在python shell中但在界面中键入名称

请帮忙,告诉我即时通讯哪里错了,即时通讯对于python来说还是新手

from collections import deque
first = deque([])
last = deque([])
nom = 0
import tkinter as tk
from tkinter import *

class Application(tk.Frame):
def __init__(self, master=None, nom=0, priono=1, prio=1):
    super().__init__(master)

    self.pack()
    self.create_widgets()
    self.nom= nom
    self.priono= priono
    self.prio=prio

def create_widgets(self):
    self.add = tk.Button(self, bg ="black", fg="pink")
    self.add["text"] =   "-----Add to queue-----\n(click me)"
    self.add["command"] = self.create_window
    self.add.pack(side="top", pady = 10, padx = 20)
    self.add["font"] = "Times 16 bold"

    self.remov = tk.Button(self, bg ="black", fg="pink")
    self.remov["text"]= "---Remove in queue---\n(click me)"
    self.remov["command"] = self.remov_
    self.remov.pack(side="top", pady = 10, padx = 20)
    self.remov["font"] = "Times 16 bold"

    self.quit = tk.Button(self, text="QUIT", fg="white", bg = "red",
    command=root.destroy)
    self.quit.pack(side="bottom")

def create_window(self):
    def inputer(self):
        self.L1 = tk.Label(self, text = "Name")
        self.L1.pack( side = LEFT)
        self.E1 = tk.Entry(self, bd =5)
        self.E1.pack(side = RIGHT)
    win2 = Toplevel(self)
    win2.button = Button(self, text='Ready?\nClick Here', command=inputer)

def say_hi(self):
    print("hi there, everyone!")

def add_1(self):
    name=input("What is your name? ")
    first.append(name)
    print("Good Day!",first[self.nom])
    print("Your priority number is:","%03d" % (self.priono))
    self.priono+=1
    self.nom+= 1

def remov_(self):
    if (len(first)) >= 1:
        first.popleft()
    else:
        print("No one left in queue")


root = tk.Tk()
root.config(background="black")

app = Application(master=root)
app.master.title("ID QUEUEING SYSTEM beta")
app.master.minsize(540, 360)
app.master.maxsize(600, 500)
app.mainloop()

1 个答案:

答案 0 :(得分:0)

您必须使用toplevel来指示小部件必须出现的位置;您还必须pack(放置或网格化)属于您顶层窗口的小部件。

inputer中的代码需要放在内部函数之外;您现在可以编写代码,让此输入器执行您需要的操作:

我删除了star导入,并在所有tk.方法调用中添加了前缀tkinter

import tkinter as tk
from collections import deque


class Application(tk.Frame):

    def __init__(self, master=None, nom=0, priono=1, prio=1):
        super().__init__(master)

        self.pack()
        self.create_widgets()
        self.nom = nom
        self.priono= priono
        self.prio=prio

    def create_widgets(self):
        self.add = tk.Button(self, bg ="black", fg="pink")
        self.add["text"] =   "-----Add to queue-----\n(click me)"
        self.add["command"] = self.create_window
        self.add.pack(side="top", pady = 10, padx = 20)
        self.add["font"] = "Times 16 bold"

        self.remov = tk.Button(self, bg ="black", fg="pink")
        self.remov["text"]= "---Remove in queue---\n(click me)"
        self.remov["command"] = self.remov_
        self.remov.pack(side="top", pady = 10, padx = 20)
        self.remov["font"] = "Times 16 bold"

        self.quit = tk.Button(self, text="QUIT", fg="white", bg = "red",
        command=root.destroy)
        self.quit.pack(side="bottom")

    def create_window(self):
        def inputer():
            print('inputer ', end=': ')
            print(self.E1.get())

        win2 = tk.Toplevel(self)
        win2_button = tk.Button(win2, text='Ready?\nClick Here', command=inputer)
        win2_button.pack()
        self.L1 = tk.Label(win2, text = "Name")
        self.L1.pack( side=tk.LEFT)
        self.E1 = tk.Entry(win2, bd =5)
        self.E1.pack(side=tk.RIGHT)

    def say_hi(self):
        print("hi there, everyone!")

    def add_1(self):
        name=input("What is your name? ")
        first.append(name)
        print("Good Day!",first[self.nom])
        print("Your priority number is:","%03d" % (self.priono))
        self.priono+=1
        self.nom+= 1

    def remov_(self):
        if (len(first)) >= 1:
            first.popleft()
        else:
            print("No one left in queue")


root = tk.Tk()
root.config(background="black")

app = Application(master=root)
app.master.title("ID QUEUEING SYSTEM beta")
app.master.minsize(540, 360)
app.master.maxsize(600, 500)
app.mainloop()