【问题标题】:reading and playing a word in the message window在消息窗口中阅读和播放单词
【发布时间】:2019-09-14 06:28:47
【问题描述】:

我的代码有问题,并且 word -> word = "" word = str (Entry.get (input_text)) 中出现错误消息,这显然是指打印全文,但无法按我想要的方式工作当我希望它在非根窗口窗口中工作但正在阅读窗口时。

这是代码:

from tkinter import *
import sys
import tkinter as tk
from tkinter import messagebox

#---------
root = Tk()
root.title("Window")
#----------

#toiminnot root ikkunassa

functions = Label(root, text = "Select an action!")
functions.place(x = 70, y= 10)

#lue toimonto koodi alkaa

def read():
    reading_window = Tk()
    reading_window.title("Read function")
    frame = Frame(reading_window)
    frame.pack()

    read_the_text = Label(reading_window, text = "Enter text in the box!")
    read_the_text.place(x = 70, y = 10)

    word = ""
    word = str(Entry.get(input_text))

#frame johon kirjoitetaan

    input_text = Entry(reading_window)
    input_text.place(x=55, y=30)

#lueikkuna koko ja sijoitus

    reading_window.geometry("300x300+100+100")

#lue sana painike joka tuo viestin

    print_button = tk.Button(reading_window, text = 'Show typed text', height = 1, width = 15) #, command = print1)
    print_button.place(x=80, y=60)

    text_a_tip = Label(reading_window, text ="The typed text is displayed in\nthe message window!")
    text_a_tip.place(x = 50, y = 90)

def print1():
    tk.messagebox.showinfo('Kirjoitetun tekstin tulostus viestiikkunaan', (Entry.get(input_text)))

    def close():
        reading_window.destroy() 

    read_close_button = tk.Button(reading_window, text = 'Close the reading function', height = 1, width = 20, command = close)
    read_close_button.place(x = 60, y = 270)

    read.mainloop()

#lue toiminto koodi loppuu

read_function = tk.Button(root, text='Read function', height = 1, width = 15, command = read)
read_function.place(x = 55,y = 35)

#ohjleman lopettamisen koodi alkaa

def quit_prog():
    MsgBox = tk.messagebox.askquestion('Quit program', ' Are you sure you want to close the program?',icon = 'warning')
    if MsgBox == 'yes':
        root.destroy()
        sys.exit(0)

    else:
        tk.messagebox.showinfo('Back','Now you are back!')

quit_programbutton = tk.Button(root, text='Close program', height = 1, width = 15, command = quit_prog)
quit_programbutton.place(x=50, y=220)

#ohjelman lopettamisen koodi loppuu tähän
#----------
#----------
root.geometry("250x250+20+60") #"450x450=leveysxkorkeus"+"20+40=vasenreuna+yläreuna"
root.mainloop()

source

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    我真的不知道什么是“word”变量,但我认为你需要它。您不能使用在您使用它的行下定义的变量。

        word = ""
        word = str(Entry.get(input_text))   # input_text is defined below, interpreter doesn't know what is it  "input_text" yet.
    
        input_text = Entry(reading_window)
        input_text.place(x=55, y=30)
    

    应该是这样的。

        input_text = Entry(reading_window)
        input_text.place(x=55, y=30)
        word = ""
        word = str(Entry.get(input_text))   # now there is no error
    

    它不能解决你的问题,因为我看到在函数 print1 中你再次使用了 imput_text 但它在不同的函数中定义,所以它不起作用。

    def print1():
        # input_text is local variable in read function.
        tk.messagebox.showinfo('Kirjoitetun tekstin tulostus viestiikkunaan', (Entry.get(input_text)))
    

    要解决您的问题,您可以将 input_text 作为 print1 的参数传递,就像在这个工作示例中一样。

    def read():
        reading_window = Tk()
        reading_window.title("Read function")
        frame = Frame(reading_window)
        frame.pack()
    
        read_the_text = Label(reading_window, text = "Enter text in the box!")
        read_the_text.place(x = 70, y = 10)
    
        input_text = Entry(reading_window)
        input_text.place(x=55, y=30)
        word = ""
        word = str(Entry.get(input_text))
        reading_window.geometry("300x300+100+100")
        # pass input_text to print1
        print_button = tk.Button(reading_window, text = 'Show typed text', height = 1, width = 15, command = lambda: print1(input_text))
        print_button.place(x=80, y=60)
    
        text_a_tip = Label(reading_window, text ="The typed text is displayed in\nthe message window!")
        text_a_tip.place(x = 50, y = 90)
    
    def print1(input_text):
        # print1 takes now one argument and has reference to input_text
        tk.messagebox.showinfo('Kirjoitetun tekstin tulostus viestiikkunaan', (Entry.get(input_text)))
    
        def close():
            reading_window.destroy() 
    
        read_close_button = tk.Button(reading_window, text = 'Close the reading function', height = 1, width = 20, command = close)
        read_close_button.place(x = 60, y = 270)
    
        read.mainloop()
    

    考虑使用不同的几何管理器而不是位置,它甚至写在 effbot doc 中:

    在普通的窗口和对话框布局中使用 place 通常不是一个好主意;只是为了让事情按应有的方式工作需要做很多工作。为此类目的使用包或网格管理器。

    Place Geometry Manager

    【讨论】:

      猜你喜欢
      • 2012-07-14
      • 2013-11-27
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多