如何使tkinter窗口保持打开状态,在其中按按钮可以打印信息

时间:2019-06-18 22:46:40

标签: python tkinter

我有一个tkinter gui。我有写到文件随机数的按钮。我想保持一个单独的窗口打开,该窗口记录所有正在写入文件的数据。因为它不会让我发布带有这么少描述的太多代码,所以这样做的目的是要使我警觉,而我按随机按钮几次是以前的描述,而没有打开文本文件。为了澄清起见,这只是代码的一部分。它有更多的窗口和随机功能,我在操作gui时还是很警惕。

# imports
import random
from random import randint
import subprocess  
import os
import shutil
import re
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
import tkinter as tk




# main window 
root = Tk(  )
root.geometry('1100x900')







global random1min
global random1max



random1min = StringVar()
random1max = StringVar()



def savedata():

    global random1min_info
    global random1min_entry
    global random1max_info
    global random1max_entry

    random1min_info = random1min.get()
    random1max_info = random1max.get()

    my_function()





def my_function():

    random1min_info = random1min.get()
    random1max_info = random1max.get()





# function to write entry to file
def random1():

    with open("C:/random1.txt", 'r') as myfile:
        text = myfile.read()
    with open("C:/write in 1.txt", "a") as myfile2:
      myfile2.write(text + random1min_info +"," + random1max_info + ")// a=min, b=max" + "\n") 


# entry window to input data
def random1entry():
    global random1min
    global random1min_entry
    global random1max
    global random1max_entry
    random1window = Toplevel(root)
    random1window.geometry('600x600')
    Label(random1window, text = "random1 min * ").grid(row=1, column=1) 
    random1min_entry = Entry(random1window, textvariable = random1min)
    random1min_entry.grid(row=2, column=1) 
    Label(random1window, text = "random1 max * ").grid(row=3, column=1)  
    random1max_entry = Entry(random1window, textvariable = random1max)
    random1max_entry.grid(row=4, column=1) 

    Label(random1window, text = "").grid(row=5, column=1) 
    Button(random1window, text = "savedata", width = 10, height = 1, command = savedata).grid(row=6, column=1) 
    Label(random1window, text = "").grid(row=7, column=1) 
    Button(random1window, text = "write function", width = 10, height = 1, command = random1).grid(row=8, column=1) 

    def clearentries():
        random1min_entry.delete(0, END)
        random1max_entry.delete(0, END)

    Button(random1window, text = "clear entries", width = 10, height = 1, command = clearentries).grid(row=20, column=1) 



Button(root, text = "random1", width = 10, height = 4, command = random1entry).grid(row=9, column=4)





menu = Menu(root)
root.config(menu=menu)

file = Menu(menu)


file.add_command(label = 'Exit', command = lambda:exit())






root.mainloop()

0 个答案:

没有答案