【问题标题】:Textbox not showing inserts in tkinter文本框未在 tkinter 中显示插入
【发布时间】:2021-07-11 22:08:20
【问题描述】:
import tkinter as tk
from tkinter import *
import time


def centertitle(e):
    w = int(root.winfo_width() / 3.5)
    s = 'Installation Wizard v1.0'.rjust(w//2)
    root.title(s)

def textbox():
    textbox=Text(root.canvas, width = 62, height = 25, state=DISABLED)
    textbox.pack()
    textbox.insert(constants.INSERT,'You text goes here')

#Configure tkinter window and title
root = Tk()
root.canvas = Canvas(root, width = 500, height = 404, bg="#3D3D3D", highlightthickness=0)
root.canvas.pack(expand=True, fill=BOTH)

root.iconbitmap(default='transparent.ico')  
root.bind("<Configure>", centertitle)
root.resizable(False, False)

#Buttons
btn = tk.Button(root, text='Start Install', height = 2, width = 15)
btn['command'] =  lambda b=btn:[b.pack_forget(), b.place_forget(), textbox()]
btn.pack(fill=BOTH)
btn.place(x=190, y=181)

root.mainloop()

我试图创建的是一个简单的“安装”gui,我打算将它打包到一个 exe 中。我真正想要的是一个按钮,它启动另一个python文件(安装所有东西的那个),当它调用打印时,我希望它发送到gui中的文本框......这很难解释,但我希望你明白我的意思是

有人可以帮助我,我真的对 guis 感到困惑

来自其他 .py 的代码:

import os
import sys
import shutil
import subprocess;
from pathlib import Path
import psutil
import time


def startProgram(program):
    SW_HIDE = 0
    info = subprocess.STARTUPINFO()
    info.dwFlags = subprocess.STARTF_USESHOWWINDOW
    info.wShowWindow = SW_HIDE
    subprocess.Popen(program, startupinfo=info)
    
def terminateProgram(processName):
    for proc in psutil.process_iter():
        if processName.lower() in proc.name().lower():
            proc.terminate()
           
def checkIfProcessRunning(processName):
    for proc in psutil.process_iter():
        try:
            if processName.lower() in proc.name().lower():
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return False;

st_inf = subprocess.STARTUPINFO()
st_inf.dwFlags = st_inf.dwFlags | subprocess.STARTF_USESHOWWINDOW
user_profile = os.environ['USERPROFILE']
appdata_local = os.environ['LOCALAPPDATA']
FolderDictionary = [(user_profile + '\spicetify-cli'), (user_profile + '\.spicetify'), (appdata_local + '\spotify')]

for x in FolderDictionary:
    try:
        shutil.rmtree(x)
        print('"%s", has been deleted.\n' % x)
    except OSError as e :
        print('"%s", was not found.\n' % x)
        
print("Installing Spotify.\n")
terminateProgram('Spotify.exe')
startProgram('.\\data\spotify-1-1-62-583.exe')
while True:
    if checkIfProcessRunning('spotify-1-1-62-583.exe') == False:
        print("Finished Installing Spotify.\n")
        terminateProgram('Spotify.exe')
        break

print("Installing Spicetify.\n")
terminateProgram('powershell')
subprocess.Popen(["powershell","$v='2.5.0'; Invoke-WebRequest -UseBasicParsing 'https://raw.githubusercontent.com/khanhas/spicetify-cli/master/install.ps1' | Invoke-Expression\nspicetify\nspicetify backup apply enable-devtool"], startupinfo=st_inf)
while True:
    if checkIfProcessRunning('powershell') == False:
        print("Finished Installing Spicetify.\n")
        terminateProgram('Spotify.exe')
        break

print("Downloading Themes.\n")
terminateProgram('powershell')
subprocess.Popen(["powershell",'$sp_dir = "${HOME}\spicetify-cli"\n$zip_file = "${sp_dir}\Themes.zip"\n$download_uri = "https://github.com/morpheusthewhite/spicetify-themes/archive/refs/heads/v2.zip"\nInvoke-WebRequest -Uri $download_uri -UseBasicParsing -OutFile $zip_file\nExpand-Archive -Path $zip_file -DestinationPath $sp_dir -Force\nRemove-Item -Path $zip_file\nRemove-Item -LiteralPath "${HOME}\spicetify-cli\Themes" -Force -Recurse\nRename-Item "${HOME}\spicetify-cli\spicetify-themes-2" "${HOME}\spicetify-cli\Themes"\nRemove-Item "${HOME}\spicetify-cli\Themes\*.*" -Force -Recurse | Where { ! $_.PSIsContainer }\nRename-Item "${HOME}\spicetify-cli\Themes\default" "${HOME}\spicetify-cli\Themes\SpicetifyDefault"'], startupinfo=st_inf) 
while True:
    if checkIfProcessRunning('powershell') == False:
        print("Finished Downloading Themes.\n")
        break

【问题讨论】:

  • 为什么会有两段代码? other.py 的目的是什么?第一个代码块没有引用它。请尽量只有一个代码块以避免混淆。
  • 为什么要将Text 小部件放在Canvas 中?此外,虽然Text 小部件的state"disabled",但没有人不能编辑它的内容。这包括你。使用.config(state="normal") 然后尝试插入您的文本

标签: python user-interface tkinter


【解决方案1】:

您已将文本小部件的状态设置为禁用,因此您无法向其中插入任何内容。如果您希望禁用它,请在插入文本之后而不是之前禁用它。

def textbox():
    textbox=Text(root.canvas, width = 62, height = 25)
    textbox.pack()
    textbox.insert(constants.INSERT,'You text goes here')
    textbox.configure(state=DISABLED)

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 2020-09-15
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2021-03-23
    相关资源
    最近更新 更多