Python Forum
[Tkinter] GUI Opens Minimized
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] GUI Opens Minimized
#11
We are confused. Your original post says this:
Quote:Since the migration the Tkinter window is opening minimized (python 3.10.4) while that was not the case under Windows 7 (python 3.8.13)
And your last post this:
Quote:I make the window appear by clicking on the minimized icon on the task bar.
I don't think you understood the question I was asking, because I really don't understand your response.

The code you post has "top.withdraw()" in line 2. This hides the window. No window, no icon. In the example code you provided there is no way to make the top window visible. That is why I asked "how do you make the window appear?" Does your real code hide the top window this way? If not, why is it in your code example? If it does hide the top window, how do you make the window appear? How did you do this on WIndows 7? Are there other windows?
Reply
#12
That call prevents a blank window from covering up other instructional windows (see attached). It does not change the way the script works. The GUI still opens minimized. I'm a Tkinter novice so all I can tell you is that using Phython 3.10 does not behave the same way as 3.8 did. If I remove that call, the GUI still opens minimized. Isn't that GUI a sub-window of the top window?

Attached Files

Thumbnail(s)
   
Reply
#13
Your example shows widgets being added to the root window (top). You would not have a blank window as appears in your screenshot. You main window must something else. Your example has something called "gui" in line 61. What is that? Is that supposed to be your top level window?

Please provide a better description of what your program does. I don't think anyone can help you unless you provide more information. The only thing we know is that it starts up iconified and you posted some code that is nothing at all like the code you are running.

See the problem?
Reply
#14
OK, Here is the entire code (Sanitized):

import win32gui as win32
import win32api
import win32con
import re
import win32process
from win32gui import GetWindowText,GetForegroundWindow
import pyperclip as CB
from datetime import datetime
import tkinter as tk
from tkinter import ttk
import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

def getPhoneTray():
    windows = []
    win32.EnumWindows(lambda hwnd, resultList: resultList.append(hwnd),
    windows)
    for window in windows:
        a=win32.GetWindowText(window)
        if (a.__contains__('PhoneTray')):
            win32.ShowWindow(window,9)
            win32.SetForegroundWindow(window)
            break
    wnd=win32.GetForegroundWindow()
    CB.copy('')
    clip=CB.waitForNewPaste()
    data=clip.split()
    number=data[len(data)-4]
    date=data[len(data)-3]
    time=data[len(data)-2]
    ampm=data[len(data)-1]
    name=data[1]
    if (data[2]!=data[len(data)-4]):
        name+=' '+data[2]
    return(name,number,date,time,ampm)

def center(win):
    """
    centers a tkinter window
    :param win: the main window or Toplevel window to center
    """
    win.update_idletasks()
    width = win.winfo_width()
    frm_width = win.winfo_rootx() - win.winfo_x()
    win_width = width + 2 * frm_width
    height = win.winfo_height()
    titlebar_height = win.winfo_rooty() - win.winfo_y()
    win_height = height + titlebar_height + frm_width
    x = win.winfo_screenwidth() // 2 - win_width // 2
    y = win.winfo_screenheight() // 2 - win_height // 2
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    win.deiconify()

def getSelect(page):
    select=page.html.find(id='ddlSubjectMatter')
    result=BeautifulSoup(str(select),'html.parser')
    selection={}
    for option in result.find_all('option'):
        if (len(option.attrs['value'])>0):
            selection[option.attrs['value']]=option.get_text()
    return(selection)

def cancel(top,browser):
    top.quit()
    browser.quit()
    exit(0)

def validatePhone(top):
    num=phone.get()
    pattern=re.compile('\d{3}-\d{3}-\d{4}')
    if (pattern.search(num)):
        top.withdraw()
        top.quit()
    else:
        phone.configure(background='Red')
    return(pattern.search(num))

def msgPanel(msg):
    panel=tk.Toplevel()
    panel.title('Do Not Call Interface')
    panel.geometry('200x100')
    tk.Label(panel,text='').grid(row=0,column=0)
    tk.Label(panel,text=msg).grid(row=1,column=1,columnspan=3)
    panel.update()
    return(panel)

top=tk.Tk()
top.withdraw()
intro=msgPanel('Copy line from Phonetray panel')
if (tk.Toplevel.winfo_exists(intro)==1):
    nme,num,dte,tim,pmam=getPhoneTray()
    if (nme.find('Spam')>=0 or nme.find('Telemarketer')>=0):
        nme='Unavaliable'
    intro.destroy()
else:
    nme='Unknown'
    num=''
contact=msgPanel('Contacting Do Not Call Web Site')
url='https://www.donotcall.gov/report.html'
options=Options()
options.add_argument("--headless")
#options.binary=FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
ser=Service(r'C:\Program Files\Python310\Scripts\geckodriver.exe')
browser=webdriver.Firefox(service=ser,options=options)
browser.get(url)
button_continue=browser.find_elements_by_css_selector('#MainContinueButton')
button_continue[0].click()
try:
    wait=WebDriverWait(browser,10)
    wait.until(EC.visibility_of_all_elements_located((By.ID,'ddlSubjectMatter')))
except (TimeoutException):
    contact.destroy()
    err=msgPanel('Do not call server too slow - try later')
    browser.quit()
    time.sleep(15)
    err.destroy()
    exit(-1)  
contact.destroy()
gui=msgPanel('Setting up user input')
topWidth=600
topHeight=200
top.title("Do Not Call Interface")
top.geometry(str(topWidth)+'x'+str(topHeight))
top.resizable(False,False)
rw=0
myPhone=tk.StringVar()
tk.Radiobutton(top,text='xxx-xxx-7932',variable=myPhone,value='xxx-xxx-7932').grid(row=rw,column=0,pady=5,columnspan=2)
tk.Radiobutton(top,text='yyy-yyy-6305',variable=myPhone,value='yyy-yyy-6305').grid(row=rw,column=2,pady=5,columnspan=2)
tk.Radiobutton(top,text='zzz-zzz-7477',variable=myPhone,value='zzz-zzz-7477').grid(row=rw,column=4,pady=5,columnspan=2)
myPhone.set(xxx-xxx-7932')
rw+=1
tk.Label(top,text='Phone',justify=tk.LEFT).grid(row=rw,column=0,pady=5,sticky=tk.W)
phone=tk.Entry(top,width=12,justify=tk.LEFT)
phone.grid(row=rw,column=1,pady=5,sticky=tk.W)
phone.insert(0,num)
phone.focus_set()
rw+=1
tk.Label(top,text='Caller Name',justify=tk.LEFT).grid(row=rw,column=0,pady=5,sticky=tk.W)
name=tk.StringVar()
nameEntry=tk.Entry(top,textvariable=name,width=48,justify=tk.LEFT)
nameEntry.grid(row=rw,column=1,pady=5,columnspan=4)
name.set(nme)
rw+=1
tk.Label(top,text='Date',justify=tk.LEFT).grid(row=rw,column=0,pady=5,sticky=tk.W)
dateTime=datetime.now()
today=tk.Entry(top,width=10,justify=tk.LEFT)
today.grid(row=rw,column=1,pady=5,sticky=tk.W)
today.insert(0,dte)
tk.Label(top,text='Time',justify=tk.LEFT).grid(row=rw,column=2,pady=5,sticky=tk.W)
now=tk.Entry(top,width=6,justify=tk.LEFT)
now.grid(row=rw,column=3,pady=5,sticky=tk.W)
ampm=tk.IntVar()
now.insert(0,tim)
if (pmam=='AM'):
    ampm.set(1)
else:
    ampm.set(2)
tk.Radiobutton(top,text='AM',variable=ampm,value=1).grid(row=rw,column=4,pady=5,sticky=tk.W)
tk.Radiobutton(top,text='PM',variable=ampm,value=2).grid(row=rw,column=5,pady=5,sticky=tk.W)
rw+=1
subjectList=Select(browser.find_element_by_css_selector('#ddlSubjectMatter'))
selections={}
for opt in subjectList.options:
    if (opt.text!=''):
        selections[opt.get_attribute('value')]=opt.text
subject=tk.StringVar()
subjectMatter=ttk.Combobox(top,textvariable=subject,width=53,justify=tk.LEFT)
subjectMatter['state']='readonly'
subjectMatter['values']=list(selections.values())
tk.Label(top,text='Subject',justify=tk.LEFT).grid(row=rw,column=0,pady=5,sticky=tk.W)
subjectMatter.grid(row=rw,column=1,columnspan=5)
subject.set('Unknown')
rw+=1
submitButton=tk.Button(top,text='Submit',command=lambda: validatePhone(top))
submitButton.grid(row=rw,column=2,pady=5,sticky=tk.W)
cancelButton=tk.Button(top,text='Cancel',command=lambda: cancel(top,browser)).grid(row=rw,column=4,pady=5,sticky=tk.W)
center(top)
gui.destroy()
top.mainloop()
sbmt=msgPanel('Submitting Report')
day=today.get()
phoneEntry=browser.find_element_by_css_selector('#PhoneTextBox')
phoneEntry.send_keys(myPhone.get())
dateEntry=browser.find_element_by_css_selector('#DateOfCallTextBox')
dateEntry.send_keys(day)
hour=Select(browser.find_element_by_css_selector('#TimeOfCallDropDownList'))
hh24=tim[0:2]
if (hh24[1:]==':'):
    hh24=tim[0:1]
if (pmam=='PM'):
    hh42=str(int(hh24)+12)
if (len(str(hh24))<2):
    hour.select_by_value('0'+str(hh24))
else:
    hour.select_by_value(str(hh24))
minute=Select(browser.find_element_by_css_selector('#ddlMinutes'))
mm=tim[3:]
if (len(mm)<2):
    mm='0'+mm
minute.select_by_value(mm)
subjectList.select_by_visible_text(subject.get())
preRecorded=browser.find_element_by_css_selector('#PrerecordMessageYESRadioButton')
preRecorded.click()
notText=browser.find_element_by_css_selector('#PhoneCallRadioButton')
preRecorded.click()
notText=browser.find_element_by_css_selector('#PhoneCallRadioButton')
notText.click()
nextStep=browser.find_element_by_css_selector('#StepOneContinueButton')
nextStep.click()
callerPhone=browser.find_element_by_css_selector('#CallerPhoneNumberTextBox')
callerPhone.send_keys(phone.get())
callerName=browser.find_element_by_css_selector('#CallerNameTextBox')
callerName.send_keys(name.get())
business=browser.find_element_by_css_selector('#HaveBusinessNoRadioButton')
business.click()
stopCalling=browser.find_element_by_css_selector('#StopCallingYesRadioButton')
stopCalling.click()
firstName=browser.find_element_by_css_selector('#FirstNameTextBox')
firstName.send_keys('Dennis')
LastName=browser.find_element_by_css_selector('#LastNameTextBox')
LastName.send_keys('nnnnnnnnn')
street=browser.find_element_by_css_selector('#StreetAddressTextBox')
street.send_keys('nnn sssss sssss Rd.')
city=browser.find_element_by_css_selector('#CityTextBox')
city.send_keys('ccccccccc')
state=Select(browser.find_element_by_css_selector('#StateDropDownList'))
state.select_by_visible_text('GA')
zipcode=browser.find_element_by_css_selector('#ZipCodeTextBox')
zipcode.send_keys('nnnnnn')
step2Submit=browser.find_element_by_css_selector('#StepTwoSubmitButton')
step2Submit.click()
try:
    wait=WebDriverWait(browser,5)
    wait.until(EC.presence_of_element_located((By.ID,'StepTwoAcceptedPanel')))
    msg='Report accepted'
except (TimeoutException):
    msg='Submission failed'
sbmt.destroy()
success=msgPanel(msg)
browser.quit()
time.sleep(3)
success.destroy()
top.destroy()
Reply
#15
When you run the code are you seeing the msgPanels that say "Copy line from Phonetray panel" or "Setting up user input"?

Have you verified that the program is reaching the line top.mainloop()?

Have you installed an updated version of win32gui?

Do you use win32api, win32con or win32process? They are imported but not referenced in the code.
Reply
#16
(Apr-22-2022, 04:38 PM)deanhystad Wrote: When you run the code are you seeing the msgPanels that say "Copy line from Phonetray panel" or "Setting up user input"?

Have you verified that the program is reaching the line top.mainloop()?

Have you installed an updated version of win32gui?

Do you use win32api, win32con or win32process? They are imported but not referenced in the code.

Keep in mind that the program works just fine with the exception that the GUI window opens minimized. I have to click it on the task bar to bring it up. That is the problem I am trying to resolve. Once everything is working I will remove the unnecessary imports. I also have some minor Selenium issues but that is for another sub-forum.
Reply
#17
Is the top window minimized when you run this:
import tkinter as tk
from time import sleep

def cancel(win):
    win.quit()
    exit(0)

def center(win):
    win.update_idletasks()
    width = win.winfo_width()
    frm_width = win.winfo_rootx() - win.winfo_x()
    win_width = width + 2 * frm_width
    height = win.winfo_height()
    titlebar_height = win.winfo_rooty() - win.winfo_y()
    win_height = height + titlebar_height + frm_width
    x = win.winfo_screenwidth() // 2 - win_width // 2
    y = win.winfo_screenheight() // 2 - win_height // 2
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    win.deiconify()

top = tk.Tk()
top.withdraw()
sleep(5)
tk.Button(top, text="Cancel", command=lambda: cancel(top)).pack(padx=20, pady=20)
center(top)
top.mainloop()
Reply
#18
(Apr-22-2022, 06:02 PM)deanhystad Wrote: Is the top window minimized when you run this:

No
Reply
#19
Then the problem isn't tkinter. I was wondering if center was getting 0 for window width or height.
Reply
#20
It has to be some difference between 3.8 and 3.10 or Windows 7 vs 10.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] MenuBar hidden when new window opens john8888 5 1,444 Sep-14-2022, 02:32 PM
Last Post: john8888
  [Tkinter] Make A Button That Opens Another Tk() Window That I Have Made Saif133 5 28,336 Jan-20-2017, 08:10 PM
Last Post: scriptso

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020