0

用户输入条目在窗口中(opnw1),显示整个故事的代码应该是什么,包括窗口中的条目(opnw1a),请给我一个示例代码。

import tkinter as tk
from tkinter import *
import PIL
from PIL import ImageTk

root = tk.Tk()

root.title("MAD LIBS STORY GENERATOR")

root.geometry("650x200")

root.configure(bg = "cyan")


w = tk.Label(root, text="WELCOME TO MAD LIBS STORY GENERATOR!" ,fg = "black", bg = "cyan", font = "Helvetica 20 bold italic")

w.pack()
   
photo1 = PhotoImage(file = r"E:\pen.png")

photoimage1 = photo1.subsample(3,3)


def opnw():
    top = Toplevel(root)
    
    top.title("CHOICES!!")
    
    top.geometry("650x200")
    
    top.configure(bg="cyan")

    s = tk.Label(top,text=" CHOOSE YOUR STORY CHOICE : ", fg="Black" , bg = "cyan" , font="Helvetica 20 bold italic")

    s.pack()

    def opnw1():
        
        top1 = Toplevel(top)
    
        top1.title("bE KIND")

        top1.geometry("400x500")

        top1.configure(bg="cyan")

        t = tk.Label(top1,text="FILL YOUR WORDS" , fg="dark blue" , bg="cyan" , font="helvetica 30 italic")

        t.pack()
        

        l1 = tk.Label(top1, text="  NOUN : " , fg="black" , bg="cyan" , font="helvetica 15 bold")

        l1.pack()
 
        e1 = tk.Entry(top1, bd=6)

        e1.pack()
        

        l2 = tk.Label(top1, text="  NOUN(PLURAL) : " , fg="black" , bg="cyan" , font="helvetica 15 bold")
           
        l2.pack()
        
        e2 = tk.Entry(top1, bd=6)

        e2.pack()

        
        l3 = tk.Label(top1, text="  NOUN : " , fg="black" , bg="cyan" , font="helvetica 15 bold")
           
        l3.pack()
        
        e3 = tk.Entry(top1, bd=6)

        e3.pack()
        

        l4 = tk.Label(top1, text="  PLACE : " , fg="black" , bg="cyan" , font="helvetica 15 bold")
           
        l4.pack()
        
        e4 = tk.Entry(top1, bd=6)

        e4.pack()
        

        l5 = tk.Label(top1, text="  ADJECTIVE : " , fg="black" , bg="cyan" , font="helvetica 15 bold")
           
        l5.pack()
        
        e5 = tk.Entry(top1, bd=6)

        e5.pack()
        

        l6 = tk.Label(top1, text="  NOUN : " , fg="black" , bg="cyan" , font="helvetica 15 bold")
           
        l6.pack()
        
        e6 = tk.Entry(top1, bd=6)

        e6.pack()

        
        def opwn1a():
            
            top3 = Toplevel(top1)
            
            top3.title("Completed story")
            
            top3.geometry("700x700")
            
            top3.configure(bg="cyan")

            def st(top3):
                l1=e1.get()
                l2=e2.get()
                l3=e3.get()
                l4=e4.get()
                l5=e5.get()
                l6=e6.get()

                story = "be kind to your"
                story += l1
                story +="-footed"
                story += l2
                story +="For a duck may ne somebody's"
                story += l3
                story += "Be kind to your"
                story += l2
                story += "in"
                story += l4
                story += "where the weather is always"
                story += l5
                story += "you may think that this is the"
                story += l6
                story += "Well It is."
            
            
                 
        d = tk.Button(top1, text="LET'S GO!!" , bd=7,fg="white" , bg="navy blue", command=lambda:[opwn1a,top3.st])

        d.pack()


    a = tk.Button(top, text="BE KIND" , bd=6 , font="helvetica" , compound = LEFT , command = opnw1)

    a.pack()




    def opnw2():
        top2 = Toplevel(top)

        top2.title("soooo")

        top2.geometry("700x300")

        top2.configure(bg="cyan")

        u = tk.Label(top2, text="fILL YOUR WORDS" , fg="dark blue" , bg="cyan" , font="helvetica 30 italic")

        u.pack()


    b = tk.Button(top, text="Sooooooo" , bd=6 , font="helvetica" , compound = RIGHT , command = opnw2)

    b.pack()
     

v = tk.Button(root, text = "Let's Create A Story!!", bd = 6 ,font = "helvetica", image = photoimage1 , compound = RIGHT , command=opnw)

v.pack()

frame = Frame(root)

frame.pack()

canvas = Canvas(frame, bg="cyan",  width=105 , height=100 , highlightthickness=0)

canvas.pack()

photoimage = ImageTk.PhotoImage(file="E:\lmf.png")


canvas.create_image(55, 47 , image=photoimage)

root.mainloop()
4

1 回答 1

0

你没试过用吗,python和.global有两个作用域。编写在主块(外部函数)中的代码是在全局范围内定义的,这意味着它可以在代码中的任何地方使用。但是在函数内部定义的变量只能在函数内部使用,因为它们是本地定义的。要使局部变量成为全局变量,只需说.localglobalglobal var_name

这是一个简单的例子来理解这一点:

from tkinter import *

root = Tk()

def main():
    global e #making it available to global scope
    new = Toplevel()
    e = Entry(root)
    e.pack()

def take():
    print(e.get()) #printing it out in another function

main() #create a new window

b = Button(root,text='Click to print whats inside the entry',command=take)
b.pack()

root.mainloop()

没有global epython 会抛出一个e未定义的错误。

于 2020-10-18T16:07:32.333 回答