Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

python-3.x - tkinter-更新刷新页面/文本以使用新的功能返回(tkinter - updating refreshing a page/text to use new return from function)

i am new to programing and have been trying to get this to work.

(我是编程的新手,一直在努力使其工作。)

ranlist() is a function that returns a random color from a tuple holding colors.

(ranlist()是从包含颜色的元组返回随机颜色的函数。)

randisk() is a function that returns a random shape from a tuple holding shapes.

(randisk()是一个从包含形状的元组返回随机形状的函数。)

Would like to be able to refresh the Page so it gives me a new return from these functions, currently it seems to be static, unless i create a new page, result will remain static on each page.

(希望能够刷新Page,以便它使我从这些函数中获得新的回报,目前看来是静态的,除非我创建一个新页面,否则结果在每个页面上都将保持静态。)

Would like to press a button and get new random color and shape from ranlist() and randisk() displayed on the page.

(想按下一个按钮,并从页面上显示的ranlist()和randisk()获取新的随机颜色和形状。)

How would/could i do this?

(我将/如何做?)

thank you for taking the time to read this.

(感谢您抽出时间来阅读。)

import tkinter as tk    
from tkinter import *
# python 3
from tkinter import font  as tkfont # python 3
import random

# containing tuple with shapes
from OneGeneralIncidentsList import GI
# containing tuple with color
from disklist import disk
#import Tkinter as tk     # python 2
#import tkFont as tkfont  # python 2

# return random shape from tuple
def ranlist():
    a=random.choice(GI)
    return a

# return random color from tuple
def randisk():
    a=random.choice(disk)
    return a



class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=14, weight="bold", slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Press Continue for random color and shape", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Continue",
                            command=lambda: controller.show_frame("PageOne"))


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        v = tk.StringVar()
        var0 = "randon color: "
        var1 = ranlist()
        var2 = "
"
        var3 = "random shape: "
        var4 = randisk()

        v.set(var0+var1+var2+var3+var4)

        self.controller = controller
        label = tk.Label(self, textvariable=v, font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)


        button = tk.Button(self, text="Next",
                           command=lambda: controller.show_frame("PageTwo"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        v = tk.StringVar()
        var0 = "randon color: "
        var1 = ranlist()
        var2 = "
"
        var3 = "random shape: "
        var4 = randisk()

        v.set(var0+var1+var2+var3+var4)


        self.controller = controller
        label = tk.Label(self, textvariable=v, font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Next",
                           command=lambda: controller.show_frame("PageOne"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
  ask by Leather_Syrup translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...