程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何让这个程序正确显示结果

发布于2022-11-19 00:44     阅读(1015)     评论(0)     点赞(12)     收藏(3)


I'm just learning Python and I don't know how to make this program to display result in label that I want and when I click button again I want to the new result replaces the previous one

I want to last class shows result in label or entry when i click 1st button and when i click it again the new result will replace previous.

This program is not finished yet. I don't want to write all code when i have problem with first function of program. Once I deal with this problem, writing the rest of the code will not be difficult

import tkinter as tk
from tkinter import ttk
  


  
class tkinterApp(tk.Tk):

    def __init__(self, *args, **kwargs):

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

        windowWidth = 300
        windowHeight = 200

        offsetLeft = int( (self.winfo_screenwidth() - windowWidth) / 2 )
        offsetTop  = int( (self.winfo_screenheight() - windowHeight) / 2 )

        self.geometry('{}x{}+{}+{}'.format(windowWidth, windowHeight, offsetLeft, offsetTop))

        self.title('Konwerter systemów liczbowych') 

        self.minsize(300, 200)

        container = tk.Frame(self, relief="ridge", width=300, height=200)
        container.pack(expand = False)
  
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)        

        self.frames = {} 

        for F in (StartPage, Decy, decBin):
  
            frame = F(container, self)

            self.frames[F] = frame
  
            frame.grid(row = 0, column = 0, sticky ="nsew")
  
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
  
  
class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = ttk.Label(self, text ="Wybierz system do którego należy Twoja liczba.")
        label.grid()        
  
        button1 = ttk.Button(self, text ="Decymalny",
        command = lambda : controller.show_frame(Decy))
        button1.grid(padx = 5, pady = 5)

        button2 = ttk.Button(self, text ="Binarny",
        command = lambda : controller.show_frame(Binar))
        button2.grid(padx = 5, pady = 5)

        button3 = ttk.Button(self, text ="Oktalny",
        command = lambda : controller.show_frame(Oktal))
        button3.grid(padx = 5, pady = 5)

        button4 = ttk.Button(self, text ="Heksadecymalny",
        command = lambda : controller.show_frame(Heksal))
        button4.grid(padx = 5, pady = 5)
  

class Decy(tk.Frame):
     
    def __init__(self, parent, controller):
         
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text ="Wybierz system na jaki chcesz przekowertować")
        label.grid()
        label = ttk.Label(self, text ="swoją liczbę.")
        label.grid()

        button1 = ttk.Button(self, text ="Binarny",
                            command = lambda : controller.show_frame(decBin))
        button1.grid(padx = 5, pady = 5)

        button2 = ttk.Button(self, text ="Oktalny",
                            command = lambda : controller.show_frame(decOkt))
        button2.grid(padx = 5, pady = 5)

        button2 = ttk.Button(self, text ="Heksadecymalny",
                            command = lambda : controller.show_frame(decHex))    
        button2.grid(padx = 5, pady = 5)

        button2 = ttk.Button(self, text ="Powrót",
                            command = lambda : controller.show_frame(StartPage))     
        button2.grid(padx = 5, pady = 5)
  

class decBin(tk.Frame):

    def clearText(self):
        self.entry1.confing(text='')

    def oblicz():        
            dec = wpis.get()
            dec = int(dec)
            i = 0
            bnum = []
            while dec!=0:
                rem = dec%2
                bnum.insert(i, rem)
                i = i+1
                dec = int(dec/2)
            i = i-1

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text ="Wprowadź liczbę i zatwierdź.")
        label.grid()
  
        wpis = ttk.Entry(self)
        wpis.grid()         

    
        button1 = ttk.Button(self, text="Konwertuj", command = oblicz)
        button1.grid(padx = 10, pady = 10)

        button2 = ttk.Button(self, text ="Powrót", command = lambda : controller.show_frame(StartPage))
        button2.grid(padx = 10, pady = 10)
  

app = tkinterApp()
app.mainloop()

解决方案


如果您想更新现有的Label小部件,请声明一个tk.StringVar()以存储标签文本,然后将其绑定到您Labeltextvariable属性。然后你会自动更新Label只要你set()StringVar

label_var = tk.StringVar(self, 'Default Value')  # both of these args are optional
label = ttk.Label(self, textvariable=label_var)  # instantiate Label and bind the var

要更新标签:

label_var.set('New String Value')

当你grid()//你的时候pack()它将以你给的文本开始,如果有的话。place()LabelStringVar



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1859821/5f931cd56faad91c80bf/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)